我在 UINavigationController 中使用带有 UITableView 的 Storyboard 。
在这个 UITableView 中,使用了具有内部属性的自定义 tableViewCell。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CustomTableViewCell *cell = nil;

    if (SYSTEM_VERSION_LESS_THAN(@"6.0") ) {

        //iOS 6.0 below
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    }
    else {
        //iOS 6.0 above

        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; //work segue

    }

上面的代码适用于 push segue。但不是当我使用
     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];   //not work segue

我使用这种 alloc 方法来保护单元格的数据免于重用单元格。

这只是 alloc 与 deque .. 方法的区别。我错过了什么?

编辑)我知道由于性能原因,不使用 dequeReusableCell 方法是不好的。但是,单元格的数量不会很多。这就是为什么我不需要 deque 方法。
  • “不工作”的意思是“不执行推送转场”,而不是崩溃。

    除了单元格右侧的显示指示器图标外,它显示的单元格与使用 dequeReusable 方法时相同。指示器图标来自 Storyboard设置。

    当我触摸该单元格时,该单元格突出显示为蓝色,但不执行 push segue。
  • CustomTableViewCell 有 4 个属性。这与 UITableViewCell 完全不同。用户在 DetailViewController 设置属性(push segue 导致这个)。该单元格没有 IBOutlet 引用。在 MasterViewController(具有 tableView)中,cellForRowAtIndexPath 方法返回上面代码的 CustomTableViewCell。
  • cellForRowAtIndexPath 方法在 CustomTableViewCell 的指示器左侧添加一个开/关按钮
    并为单元格设置标签编号。
  • 最佳答案

    dequeueReusableCellWithIdentifier 的使用使您能够使用原型(prototype)单元。如果您使用 initWithStyle 而不是 dequeueReusableCellWithIdentifier ,那么您不会这样做,因此您也会丢失任何为这些单元格原型(prototype)定义的转场、披露指示符和其他 UI 外观。

    如果你决定走这条路,你将不得不去“老派”(即做我们在细胞原型(prototype)之前曾经做过的事情)并编写自己的 didSelectRowForIndexPath 。但是,如果您已经定义了该 segue,假设您将其称为“SelectRow”,那么您的 didSelectRowForIndexPath 可以执行该操作:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
        [self performSegueWithIdentifier:@"SelectRow" sender:cell];
    }
    

    如果您需要公开指示符,那么您的自定义单元格例程(或 cellForRowAtIndexPath )将必须手动设置。如果你添加它
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    

    然后你需要手动处理它:
    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
        [self performSegueWithIdentifier:@"SelectAccessory" sender:cell];
    }
    

    最重要的是,你可以让它工作,但你只是做了很多额外的工作,失去了出列单元格的性能和内存优势。我衷心鼓励您重新考虑不使用 dequeueCellWithIdentifier 的决定。

    关于ios - segue 不适用于 UITableViewCell 分配,但 dequeueReusableCellWithIdentifier,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15015543/

    10-11 22:15
    查看更多