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



我正在尝试实现一个表视图,其中所有行都有两个按钮,然后对它们所在的索引行的数据进行处理。

这是我目前所拥有的:

- (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");

}

然而,我不知道如何找出所选按钮被按下的索引行,以便获得相关数据。

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

denyButton.tag = 1000 + indexPath.row;

然后点击按钮:

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

创建一个方法wakeFromNib(如果你正在使用情节串连板)

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

然后在你的单元格ForRowAtIndexPath在你的视图控制器中添加:

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中测试过这一点,如果它没有编译

,我很抱歉

为什么不向后遍历视图层次结构并检查按钮的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);
}

最新更新