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

Khl*_*don 1 objective-c uitableview ios

我正在尝试实现一个表视图,其中所有行都有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");


}
Run Code Online (Sandbox Code Playgroud)

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

Mat*_*s R 5

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

denyButton.tag = 1000 + indexPath.row;
Run Code Online (Sandbox Code Playgroud)

然后在denyButtonPressed上:

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

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

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

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

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

@protocol NotificationCellDelegate
- (void)denyActionForNotificationObject:(NotificationObject *)notificationObject;
- (void)acceptActionForNotificationObject:(NotificationObject *)notificationObject;
@end
Run Code Online (Sandbox Code Playgroud)

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

@property (nonatomic, strong) NotificationObject *notificationObject;
@property (nonatomic, strong) id<NotificationCellDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)

创建方法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];
}
Run Code Online (Sandbox Code Playgroud)

实现您声明的选择器:

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

- (void)AcceptButtonPressed:(id)sender {
    if (_delegate) {
        [_delegate acceptActionForNotificationObject:_notificationObject];
     }
}
Run Code Online (Sandbox Code Playgroud)

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

cell.notificationObject = notificationObject;
cell.delegate = self;
Run Code Online (Sandbox Code Playgroud)

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

- (void)denyActionForNotificationObject:(NotificationObject *)notificationObject {
    // Do something with the notification object
}
- (void)acceptActionForNotificationObject:(NotificationObject *)notificationObject {
    // Do something with the notification object
}
Run Code Online (Sandbox Code Playgroud)

我没有在XCode中测试过这个,如果不编译,我很抱歉