【问题标题】:iOS - Dilemna - how to segue from customized cellsiOS - Dilemna - 如何从自定义单元格中分离
【发布时间】:2014-09-25 01:31:50
【问题描述】:

我有一个从视图控制器(带有封闭的 tableview)到另一个视图控制器的 segue。我可以毫无问题地从第一个控制器中的每个单元格转到第二个单元格。但是,当我返回第一个控制器时,单元格视图是空白的。

困境是——如果我使用这种方法:

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

单元格可见,但 segues 不起作用。

有人知道另一种方法吗?

谢谢

【问题讨论】:

  • 那么不要使用shouldHighlightRowAtIndexPath 它不是为了那个,你如何执行segue?请提供更多代码

标签: ios objective-c uitableview storyboard segue


【解决方案1】:

您需要创建自定义表格视图单元格(可能您已经有一个,然后调整它),我们称之为MyTableViewCell。然后添加UITapGestureRecognizer 来处理单元格contentView 上的点击事件。当点击发生时,您可以执行自定义块,您应该为单元格设置。在块中,您可以执行所需的 segue。话不多说,看代码吧!

首先,让我们定义 MyTableViewCell

MyTableViewCell.h

typedef void (^MyTableViewCellTapBlock) ();

@interface MyTableViewCell : UITableViewCell
@property (nonatomic, strong) MyTableViewCellTapBlock tapBlock;
@end

MyTableViewCell.m

@interface MyTableViewCell ()
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
@end

@implementation MyTableViewCell

- (void)awakeFromNib {
    self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self.contentView addGestureRecognizer:self.tapRecognizer];
}

- (void)handleTap:(UITapGestureRecognizer *)recognizer {
    NSLog(@"Tap logged");
    if (self.tapBlock) {
        self.tapBlock();
    }
}

@end

其次,更新你的 UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    __weak typeof(self) weakSelf = self;
    cell.tapBlock = ^ {
        [weakSelf performSegueWithIdentifier:@"showDetail" sender:weakSelf];
    };

    return cell;
}

备注

如您所见,我们有自定义块,当用户点击单元格时将执行该块。这个块调用performSegueWithIdentifier:,只是不要忘记命名你的segue并在示例中更改名称。

编码愉快:)

【讨论】:

    【解决方案2】:

    感谢 Keenie 的努力。我会保留该代码 sn-p,我知道我会使用它。

    这很尴尬,但事实证明,我需要做的只是从 segue 返回,做 [[self tableView] reloadData],一切都很好..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多