Obj-C:委派模式,将自己设置为委派



我有一个在TableView中使用的自定义TableView单元格-我决定需要将它的一些方法委托给tableViewController。

//Custom tableview cell
typedef enum {
    kButtonSelected,
    kButtonNormal
} ButtonState;
// Classes using this custom cell must implement these methods
@protocol CustomTableViewCellDelegate <NSObject>
@required
- (void) storeButtonState:(ButtonState)state forButton:(UIButton *)sender;
- (void) restoreButtonState:(UIButton *)tagLectureButton;
@end
@interface CustomTableViewCell : UITableViewCell
// Delegate
@property (assign) id <CustomTableViewCellDelegate> delegate;
@property (weak, nonatomic) IBOutlet UILabel *mainLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *spinner;
@property (weak, nonatomic) IBOutlet UIButton *tagLectureButton;

然后我将TableView设置为它的委托并实现这些方法。到目前为止还不错

@interface LecturesSubTVC : LecturesTVC <CustomTableViewCellDelegate>

但我没有实例变量来设置(self.CustomTableViewCell.delegate = self;)

所有单元格都是在这样的方法中分配的:

CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

如何将单元格设置为委托,以便调用委托的方法?

您似乎已经准备好了一切。在中对单元格进行排队后

CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

只需使用cell.delegate = self; 设置其代理

CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.delegate = self;

最新更新