ios - 根据它使用的 UITableViewCell 类型更改 heightForRowAtIndexPath?

标签 ios objective-c uitableview

cellForRowAtIndexPath 中,我使用随机化来创建两种不同的自定义 UITableViewCell 类型之一,我们称它们为 LCImageCell LCTextCell(一个包含图像,一个包含一些文本,并且它是随机的,将显示在每一行中)。基本布局如下:

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    // Determine whether the cell should contain an image or text..
    BOOL isCellAnImage;
    int randomChanceOfImageAppearing = arc4random() % 5;
    if (randomChanceOfImageAppearing == 4) isCellAnImage = YES;
    else isCellAnImage = NO;

    // If the cell is going to contain an image..
    if (isCellAnImage) {
        LCIImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier: @"ImageCell"];
        if (imageCell == nil) {
            imageCell = [[LCImageCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"ImageCell"];
        }

        return imageCell;

    // Else the cell will contain text..
    } else {
        // Make and allocate the cell if necessary.
        LCTextCell *customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
        if (customCell == nil) {
            customCell = [[LCTextCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
        }
        return customCell;
    }
}

我需要动态设置那些带有文本的高度(LCTextCell 实例),并且它工作正常。我现在要整合图像单元格,我想知道如何让 heightForRowAtIndexPath 知道所讨论的单元格是 LCImageCell 还是 LCTextCell ,这样我只能在相关单元格是 LCTextCell 时应用高度调整。

我可以在设置高度之前访问应用高度的单元格吗?它是否在那个时间点被创建/分配/初始化?

最佳答案

看似合乎逻辑的事情是从 -tableView:heightForRowAtIndexPath: 中调用 -cellForRowAtIndexPath:。然而,这是一种糟糕的形式(出于某种原因,后者在前者之前被调用)并且可能导致性能问题(heightForRowAtIndexPath 被调用表中的每个单元格,甚至不可见的,每次显示表格时)。

相反,将单元格随机化移动到 viewController 生命周期的早期。例如,假设您事先知道每种单元格类型的高度(并且您的 tableView 有一个单独的部分):

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.tableView numberOfRowsInSection:0]; i++) {
        if (arc4random() % 5 == 4) {
            [mutableArray addObject:[LCImageCell class]];
        } else {
            [mutableArray addObject:[LCImageCell class]];
        }
    }

    self.cellTypes = [NSArray arrayWithArray:mutableArray];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.cellTypes objectAtIndex:indexPath.row] isEqual:[LCImageCell class]]) {
        [LCImageCell height];  // class method returns static height for an image cell
    } else {
        [LCTextCell height];   // class method returns static height for a text cell
    };
}

如果高度是动态的,你应该calculate and store that in advance as well .

关于ios - 根据它使用的 UITableViewCell 类型更改 heightForRowAtIndexPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14978858/

相关文章:

objective-c - 最前面的窗口使用 CGWindowListCopyWindowInfo

ios - UIImageView 的深层复制没有复制 iOS 中应用于原始 View 的所有委托(delegate)和手势

ios - 比较值并在 uitableview 中显示特定结果

ios - 升级后 Alamofire 不工作(Swift/Xcode 8)

ios - 秒表在后台 sleep 模式下运行

objective-c - 无法撤消多个操作

ios - 关闭之前的 VC 后仅重新加载新数据

ios - 如何将自定义类添加到我的 UITableViewController?

objective-c - iOS – NSMutableArray 和 block 复制

ios - bundle 内容和文件内容之间的访问速度差异