单击按钮时单元格如何滑动

时间:2015-03-26 12:10:09

标签: objective-c ios8

我想点击一个按钮来滑动单元格。我成功刷了一个牢房。但是我想要按下单元格中的按钮。我的代码是

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

2 个答案:

答案 0 :(得分:3)

我认为您的班级SimpleCell应该包含UIButton,以便正确执行重复使用。

如果您创建一个自定义UITableViewCell,其中包含单元格上的所有UI操作并在单元格中操作它们,而不是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 以及GitHub上的SWTableViewCell

答案 1 :(得分:1)

您可以将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;
        }