您可以爲SweepStakeTableViewCell創建一個委託併爲其分配SweepViewController。
@protocol SweepStakeTableViewCellDelegate <NSObject>
- (void)sweepTableViewCell:(SweepStakeTableViewCell *)cell buttonSelected:(UIButton *)button;
@end
創建在表視圖委託方法的細胞時分配視圖控制器(在這種情況下自)到SweepStakeTableViewCell委託方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[SweepStakeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
.
.
.
cell.delegate = self;
return cell;
}
對於SweepStakeTableViewCell
@interface SweepStakeTableViewCell : UITableViewCell
.
.
@property (nonatomic, assign) id<SweepStakeTableViewCellDelegate> delegate;
.
@end
@implementation SweepStakeTableViewCell
.
- (void)buttonTapped:(id)sender
{
if ([delegate respondsToSelector:@selector(sweepTableViewCell:buttonSelected:)])
{
[delegate sweepTableViewCell:self buttonSelected:sender];
}
}
.
@end
當按鈕被在表視圖細胞選擇,它將調用在這種情況下與細胞視圖控制器和選擇的按鈕,可以委託方法檢索值。