簡體   English   中英

Objective-C自定義表格單元格按鈕

[英]Objective-c custom table cell buttons

我正在嘗試實現一個表視圖,其中所有行都有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");


}

但是我不確定如何找出所選按鈕被按下到哪個索引行,以便獲得相關數據。

最簡單的解決方案是為每個按鈕分配一個標簽。 例如:

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將把所需的任何對象傳遞給視圖控制器。

例如,在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中測試過,如果無法編譯,我深表歉意

為什么不向后瀏覽視圖層次結構並檢查按鈕的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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM