ios - 为什么 awakeFromNib 从 TableView 中的 Cell 调用两次?

标签 ios objective-c uitableview xib

我试图理解为什么 awakeFromNib 在我的代码中被调用了两次。我目前有一个 tableview,它有一个特殊的可压缩单元格,该单元格在表格末尾出现一次。当 tableview 滚动到最后的特殊单元格时,第一个 awakeFromNib 被调用(我相信这很好,因为 tableview 正在重用单元格)。但是,每当我点击单元格以展开单元格时,就会再次调用 awakeFromNib。

谁能向我解释为什么 awakeFromNib 被调用了两次?我如何才能让它只被调用一次?

谢谢

编辑**人们要求的代码

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

    if (indexPath.section >= (NSInteger)[self.trip.destinations count]) {
        GuestCell *cell = (GuestCell*)[tableView dequeueReusableCellWithIdentifier:GuestCellIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setupCellForGuests:self.trip.guests];
        cell.guestExpanded = NO;
        NSLog(@"RETURNING CELL");
        return cell;
    }

    // For all other sections
    return [self prepareCardCellForIndexPath:indexPath forHeightCalc:NO];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section >= (NSInteger)[self.trip.destinations count]) {
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

最佳答案

您正在为展开行的重新加载设置动画。 TableView 通过为相同的索引路径创建另一个单元格,并为从旧单元格到新单元格的转换设置动画来实现这一点。它会创建您的单元格原型(prototype)的第二个实例,因此第二个实例也会收到 awakeFromNib 消息。如果您登录 self,您将看到第二次地址不同。

我不认为您可以避免创建第二个单元格实例(以及第二个 awakeFromNib),除非您摆脱动画。即便如此,我也不确定它是否会重用旧电池。

关于ios - 为什么 awakeFromNib 从 TableView 中的 Cell 调用两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26223126/

相关文章:

ios - 无法将操作连接到 Xcode 6.1.1 中的现有函数

ios - 手指绘制的运动路径

iphone - 如何在 iTune Connect 上为我的 iPhone 应用程序设置自动续订应用程序内购买?

objective-c - 如何在 View Controller 中从 NSDictionary 添加和检索数据

ios - 不应该出现的快速转换错误

ios - UITableviewCell 高度未在滚动时重置

objective-c - 更改选定 UITableViewCell 子类中的元素

ios - 如何将 .c 文件包含到我的 ios 应用程序中?

ios - ARC 及其工作原理。

ios - UIView UIScrollview - 框架、边界、中心混淆