1.最简单的,给cell赋值cell.objc = self;
[self.objc.navigationControllerpushViewController:knowledgeVCanimated:YES];
2.通过代理方法
cell.h
@protocolMessageCellDelegate
- (void)shareBtnAction:(UIButton*)sender;
@end
[self.pushBackVaddSingleTap:self.delegate action:@selector(shareBtnAction:)];
3.通过cell的block
//TableViewCell的.h中:
#import
typedef void(^BlockButton)(NSString *str);
@interface TableViewCell : UITableViewCell
//block属性
@property (nonatomic, copy) BlockButton button;
//自定义block方法
- (void)handlerButtonAction:(BlockButton)block;
@end
//TableViewCell的.m中:
//与cell上的button关联的点击事件
- (IBAction)buttonOnCellAction:(UIButton *)sender {
if (self.button) {
self.button(self.buttonOnCell.titleLabel.text);
}
}
//block的实现部分
- (void)handlerButtonAction:(BlockButton)block
{
self.button = block;
}
//TableViewController的.m文件中: 其中self.arr 是一个定义为属性的可变数组 里边随意存储一些数据 用来在button上显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"buttonCell" forIndexPath:indexPath];
cell.title.text = [NSString stringWithFormat:@"%d" ,indexPath.row];
[cell.buttonOnCell setTitle:[self.arr objectAtIndex:indexPath.row] forState:UIControlStateNormal];
[cell handlerButtonAction:^(NSString *str) {
NSLog(@"====%@", str);
}];
// Configure the cell...
return cell;
}
4.添加手势获取指定cell
MineTableViewCell*sectionCell = [myTableViewcellForRowAtIndexPath:indexPath];
//获取indexpath
- (NSIndexPath*)currentCellIndexPath:(UIView*)view
{
UIView* obj = view;
while (![objisKindOfClass:[MessageCellclass]]) {
obj = obj.superview;
}
MessageCell* cell = (MessageCell*)obj;
NSIndexPath* indexPath = [_tbViewindexPathForCell:cell];
return indexPath;
}