Подтвердить что ты не робот

Как прокрутка ячейки при нажатии кнопки

Я хочу щелкнуть ячейку нажатием кнопки. Я успешно разбираюсь в клетке. Но я хочу пронести по кнопке, которая находится в ячейке. мой код

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleCell";
    SimpleCell *cell = (SimpleCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    SimpleCell *cekks=[[SimpleCell alloc]init];
    cekks.scrollButton.alpha =0.0;

    NSString *titleString;
    UIButton *sender = [[UIButton alloc]init];
    //[sender setBackgroundImage:[UIImage imageNamed:@"swipe.png"] forState:UIControlStateNormal];
    sender.tag = indexPath.row;

    titleString [email protected]"Send A Gift";
    UITableViewRowAction *sendAGift = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:titleString handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
       // [self deleteMail:[NSArray arrayWithObject:indexPath]:YES];

    }];

    [sendAGift setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"swipe.png"]]];



    return @[sendAGift];
}
4b9b3361

Ответ 1

Я думаю, что ваш класс SimpleCell должен содержать UIButton, чтобы правильно выполнить повторное использование.

Это будет проще, если вы создадите пользовательский UITableViewCell, который содержит все действия пользовательского интерфейса над ячейкой и будет управлять ими внутри ячейки, а не только в UITableView.

Посмотрим на пример:

Здесь файл customCell.h:

@interface customCell : UITableViewCell {
    UIButton *buttonSwipe;
}
@end

Здесь файл customCell.h:

#import "customCell.h"

@implementation customCell

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }

    return self;
}

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self commonInit];
    }

    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }

    return self;
}

- (void) commonInit {
    buttonSwipe = [UIButton... // Initialize here your button
    [buttonSwipe addTarget:self action:@selector(swipeCell:) forControlEvents:UIControlEventTouchUpInside]; 
}

- (void) swipeCell {
    // Embed here the code that makes the effect of swipe the cell.
}

Это быстропротаиваемый обходной путь с некоторым предопределенным кодом, но я думаю, что это рабочий пример, если вы хотите, чтобы ваши ячейки прокручивали ваш собственный код.

Но если вы хотите более быстрый способ, я рекомендую вам посетить Chris Wendel и его SWTableViewCell на GitHub

Ответ 2

Вы можете вызвать editActionsForRowAtIndexPath: метод как

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:SELECTED_ROW_INDEX inSection:0];
    [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath];

попробуйте приведенный ниже код

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];


    //swipe button allocation
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
            btn.tag = indexPath.row;
            [cell.contentView addSubview:btn];
            [btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
            cell.textLabel.text = [NSString stringWithFormat:@"%lu",indexPath.row];
            return cell;
        }
        -(void)buttonTouched:(id)sender{

            UIButton *btn = (UIButton *)sender;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0];
            [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath];
        }
        - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
            // Return NO if you do not want the specified item to be editable.
            return YES;
        }

        - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
            if (editingStyle == UITableViewCellEditingStyleDelete) {
                [self.objects removeObjectAtIndex:indexPath.row];
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            } else if (editingStyle == UITableViewCellEditingStyleInsert) {
                // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
            }
        }

        -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
            NSLog(@"edit");
            // code
            return nil;
        }