Objective-c自定义表格单元格按钮

时间:2014-04-10 20:37:19

标签: ios objective-c uitableview

我试图实现一个表视图,其中所有行都有2个按钮,然后对它们所在的索引行的数据执行某些操作。

这是我到目前为止所做的:

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

    if (cell == nil) {
        cell = [[NotificationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NotificationObject *notification = nil;
    notification = [_notificationArray objectAtIndex:indexPath.row];



    cell.profileImage.image = notification.profileImage;
    cell.profileImage.layer.cornerRadius = cell.profileImage.frame.size.height /2;
    cell.profileImage.layer.masksToBounds = YES;
    cell.profileImage.layer.borderWidth = 0;
    cell.detailTextView.text = notification.action;

    UIButton *denyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //set the position of the button
    denyButton.frame = CGRectMake(cell.frame.origin.x + 285, cell.frame.origin.y + 20, 23, 23);
    [denyButton setBackgroundImage:[UIImage imageNamed:@"DenyRequest.png"] forState:UIControlStateNormal];
    [denyButton addTarget:self action:@selector(denyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    denyButton.backgroundColor= [UIColor clearColor];
    [cell.contentView addSubview:denyButton];

    acceptButton.frame = CGRectMake(cell.frame.origin.x + 240, cell.frame.origin.y + 20, 23, 23);
    [acceptButton setBackgroundImage:[UIImage imageNamed:@"AcceptRequest.png"] forState:UIControlStateNormal];
    [acceptButton addTarget:self action:@selector(AcceptButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    acceptButton.backgroundColor= [UIColor clearColor];
    [cell.contentView addSubview:acceptButton];

    return cell;
}

-(void)denyButtonPressed:(id)sender{

    NSLog(@"buttonPressedDeny");


    }

-(void)AcceptButtonPressed:(id)sender{

    NSLog(@"buttonPressedAccept");


}

但是我不知道如何找出所选按钮被按下的索引行,以便我可以获取相关数据。

2 个答案:

答案 0 :(得分:5)

最简单的解决方案是为每个按钮分配一个标签。例如:

denyButton.tag = 1000 + indexPath.row;

然后在denyButtonPressed上:

-(void)denyButtonPressed:(id)sender{
     UIButton *b = (UIButton *)sender;
     NSInteger row = b.tag - 1000;
     NSLog(@"buttonPressedDeny: %d", row);
}

变量行将保存按下按钮的索引路径行。添加1000是为了避免与您可能已有的其他视图发生冲突。

让我强调一下,这是SIMPLEST解决方案,但从设计/架构的角度来看并不是最友好的。

更精细的解决方案可能是将按钮作为NotificationCell的一部分,让NotificationCell成为这些按钮的委托,并创建一个允许您的视图控制器成为每个NotificationCell的委托的协议。然后当按下按钮时,它将由NotificationCell处理,它将向视图控制器传递所需的任何对象。

例如,在NotificationCell.h中创建以下协议

@protocol NotificationCellDelegate
- (void)denyActionForNotificationObject:(NotificationObject *)notificationObject;
- (void)acceptActionForNotificationObject:(NotificationObject *)notificationObject;
@end

另外添加NotificationCell添加一个属性来保存通知和委托:

@property (nonatomic, strong) NotificationObject *notificationObject;
@property (nonatomic, strong) id<NotificationCellDelegate> delegate;

创建方法awakeFromNib(如果您使用的是故事板)

- (void)awakeFromNib {
    [super awakeFromNib];
    UIButton *denyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //set the position of the button
    denyButton.frame = CGRectMake(self.contentView.frame.origin.x + 285, self.contentView.frame.origin.y + 20, 23, 23);
    [denyButton setBackgroundImage:[UIImage imageNamed:@"DenyRequest.png"] forState:UIControlStateNormal];
    [denyButton addTarget:self action:@selector(denyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    denyButton.backgroundColor= [UIColor clearColor];
    [self.contentView addSubview:denyButton];

    acceptButton.frame = CGRectMake(self.contentView.frame.origin.x + 240, self.contentView.frame.origin.y + 20, 23, 23);
    [acceptButton setBackgroundImage:[UIImage imageNamed:@"AcceptRequest.png"] forState:UIControlStateNormal];
    [acceptButton addTarget:self action:@selector(AcceptButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    acceptButton.backgroundColor= [UIColor clearColor];
    [cell.contentView addSubview:acceptButton];
}

实现您声明的选择器:

- (void)denyButtonPressed:(id)sender {
    if (_delegate) {
        [_delegate denyActionForNotificationObject:_notificationObject];
     }
}

- (void)AcceptButtonPressed:(id)sender {
    if (_delegate) {
        [_delegate acceptActionForNotificationObject:_notificationObject];
     }
}

然后在视图控制器的cellForRowAtIndexPath中添加:

cell.notificationObject = notificationObject;
cell.delegate = self;

同样在视图控制器中,实现协议:

- (void)denyActionForNotificationObject:(NotificationObject *)notificationObject {
    // Do something with the notification object
}
- (void)acceptActionForNotificationObject:(NotificationObject *)notificationObject {
    // Do something with the notification object
}

我没有在XCode中对此进行测试,如果它没有编译,我表示道歉

答案 1 :(得分:2)

为什么不在视图层次结构中向后工作并检查按钮的superview,它应该是表格视图单元格的内容视图。谁superview应该是细胞?

-(void)denyButtonPressed:(id)sender{
     UIButton *button = (UIButton *)sender;
     UIView *contentView = button.superview;
     UITableViewCell *cell = contentView.superview;
     NSIndexPath * indexPath = self.tableView indexPathForCell:cell];
     NSLog(@"row containing button: %d", indexPath.row);
}
相关问题