首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS UITableViewCell 按鈕點擊事件傳遞:使用 Delegate 模式實現 Cell 與 Controller 通訊

     編輯:程式設計綜合問答

問題背景

在 iOS 開發中,常見的場景是自定義 UITableViewCell 內包含按鈕,點擊按鈕時需要在所屬的 View Controller 中執行具體邏輯(如彈出警報、跳轉頁面等)。由於 Cell 是複用的,且不直接持有 Controller 的引用,直接在 Cell 內調用 Controller 方法並不合適。

解決方案:Delegate 協議模式

通過定義一個協議,讓 Cell 持有弱引用的 delegate,點擊事件觸發時調用 delegate 方法,由 Controller 實現具體業務邏輯。這樣既解耦又符合 MVC 架構。

1. 定義協議並添加 delegate 屬性

在 CustomCell.h 中聲明協議與屬性:

@protocol CustomCellDelegate <NSObject>
- (void)showTitle:(NSString *)title;
@end

@interface CustomCell : UITableViewCell
@property (nonatomic, weak) id<CustomCellDelegate> delegate;
@property (nonatomic, strong) IBOutlet UIButton *imageButton;
@property (nonatomic, strong) IBOutlet UILabel *theTitle;
- (IBAction)imageButtonAction;
@end

2. Cell 實現按鈕點擊調用 delegate

在 CustomCell.m 中合成 delegate,並在點擊事件中安全調用:

@implementation CustomCell
@synthesize delegate;

- (IBAction)imageButtonAction {
    if ([self.delegate respondsToSelector:@selector(showTitle:)]) {
        [self.delegate showTitle:self.theTitle.text];
    }
}
@end

3. Controller 遵循協議並設置 delegate

在 MyViewController.h 中聲明遵循協議:

@interface MyViewController : UIViewController <CustomCellDelegate>
@end

在 MyViewController.m 的 cellForRowAtIndexPath 中設置 delegate:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    cell.delegate = self; // 關鍵:指定當前 Controller 為代理
    // 配置 cell 其他內容...
    return cell;
}

// 實現協議方法
- (void)showTitle:(NSString *)title {
    NSLog(@"The cell title is: %@", title);
    // 此處可彈出 Alert、跳轉頁面等
}

優勢總結

  • 低耦合:Cell 不需要知道 Controller 具體類,只依賴協議。
  • 易測試:可輕鬆模擬 delegate 進行單元測試。
  • 符合 Apple 設計規範:UITableView 的 dataSource/delegate 即是此模式典範。

採用 Delegate 模式,能優雅解決 Cell 內按鈕事件向外傳遞的問題,是 iOS 開發必備技能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved