Как плъзнете клетката при щракване върху бутон

Искам да плъзна клетка при щракване върху бутон. Успях да плъзна клетка. Но искам да прекарам пръст върху бутона, който е в клетката. моят код е

- (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 =@"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];
}

person varun    schedule 26.03.2015    source източник
comment
какво искаш ? Плъзнете върху бутона Щракнете или Плъзнете върху бутона, което кара клетката да плъзне   -  person Vinay Jain    schedule 22.04.2015
comment
При щракване върху бутон в клетка Плъзнете   -  person VARUN SINGHAL    schedule 22.04.2015
comment
Плъзнете клетката, когато щракнете върху бутона, нали?   -  person Vinay Jain    schedule 22.04.2015
comment
stackoverflow.com/questions/26766243/ това е точно това, което искате   -  person Vinay Jain    schedule 22.04.2015
comment
Имам нужда от щракване върху бутон, но не съм избрал метод на таблицата.   -  person VARUN SINGHAL    schedule 22.04.2015


Отговори (2)


Мисля, че вашият клас 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.
}

Това е бързо непроверено решение с някакъв предварително дефиниран код, но мисля, че е работещ пример, ако искате да накарате клетките си да плъзгат с вашия собствен код.

Но ако искате по-бърз начин, препоръчвам ви да посетите Крис Уендел и неговите SWTableViewCell в GitHub

person Jorge Ramos    schedule 22.04.2015
comment
в метода на swipecell как наричаме делегатния метод на таблицата ... Не, не ми трябва това - person VARUN SINGHAL; 22.04.2015
comment
Мммм, не виждам причината, поради която искате да извикате делегирани методи на UITableView от клетка... Можете ли да го обясните, моля? - person Jorge Ramos; 22.04.2015
comment
в iOS 8 при плъзгане на клетка има метод - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath * )indexPath - person VARUN SINGHAL; 22.04.2015
comment
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath - person VARUN SINGHAL; 22.04.2015
comment
Виждам. В такъв случай ви препоръчвам да направите делегат на вашата customCell и да го извикате в метода swipeCell. Въпреки че, ако поведението на персонализираните ви клетки не е стандартът на iOS8 (и не е, защото искате бутон за плъзгане на клетката), препоръчвам ви да създадете свой собствен код за плъзгане на клетките. - person Jorge Ramos; 22.04.2015
comment
Тук можете да намерите пълен урок за това: raywenderlich.com/62435/ - person Jorge Ramos; 22.04.2015

Можете да извикате 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;
        }
person Neenu    schedule 24.04.2015
comment
editActionsForRowAtIndexPath извиква, но не показва бутоните за опции - person Patel Jigar; 25.04.2016
comment
предполагам, че нещо липсва - person Patel Jigar; 25.04.2016