如何从自定义UITableViewCell触发segue - iOS7

时间:2013-10-06 06:49:02

标签: uitableview ios7 segue xcode5

这里我有一个问题,想要从自定义UITableViewCell中触发segue。

问题是我已经实现了一个'edit'UIButton,它将UITableViewCell的NSIndexPath传递给下一个ViewController,以便从Core Data编辑一些实体。

因此,所有方法和IBActions链接都是在UITableViewClass中实现的,而不是像往常一样在UITableViewClass中实现。通常,我会从ViewController.m文件中触发[self performSegue: withIdentifier:],但是由于IBAction方法是在UITableViewCell.m文件中实现的,因此无法访问其ViewController;后来使[self performSegue: withIdentifier:]不可能。

我认为这很常见,但我仍然无法想出解决这个问题的好主意。你有关于这个问题的任何策略吗?

1 个答案:

答案 0 :(得分:1)

我通常会对这样的小事使用回调块,因为它比委托更简洁:

@interface MyCell : UITableViewCell
@property (strong, nonatomic) void(^tapHandler)();
- (IBAction)buttonTapped;
@end

#import "MyCell.h"
@implementation MyCell

- (void)buttonTapped
{
    if (self.tapHandler) {
        self.tapHandler();
    }
}

@end

然后在配置单元格时设置点击处理程序:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = ...;
    [cell setTapHandler:^{
        //tap handling logic
    }];
}