UITableView中的多个重用标识符 [英] Multiple reuse identifiers in a UITableView

查看:78
本文介绍了UITableView中的多个重用标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,其中单元格具有可变的高度.这会导致具有重用标识符的问题,但是我真的很想要Apple给我的UITableViewCells的缓存.因此,我尝试制作一个变量重用标识符,这看起来似乎不错,但我不确定这是否正确.

I have a table view where the cells have a variable height. This causes problems with having a reuse identifier but I would really like the cache for UITableViewCells that Apple gave me. Therefore I tried making a variable reuse identifier and it seems but I'm not sure if it's the right way.

有人可以告诉我是否正在处理多个重用标识符吗?

Can anyone tell me if I'm handling multiple reuse identifiers right?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = [self calcCellHeight:indexPath];

    NSString *CellIdentifier = [NSString stringWithFormat:@"TextCell_%f", height];

    TextCell *textCell = (TextCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (textCell == nil) {
        textCell = [[TextCell alloc] initWithHeight:height reuseIdentifier:CellIdentifier];
    }

    return textCell;
}

推荐答案

执行此操作的最佳方法可能是每次获取单元时都设置高度,然后重新计算该设置器中的所有内部单元框架.这是一个示例:

The best way to do this is probably to set the height every time you get the cell, and then recalculate all the internal cell frames in that setter. Here's an example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = [self calcCellHeight:indexPath];

    TextCell *textCell = (TextCell *)[tableView dequeueReusableCellWithIdentifier: @"AlwaysTheSame"];

    if (textCell == nil) {
        textCell = [[[TextCell alloc] init] autorelease];
    }
    [textCell setHeight: height];

    return textCell;
}

另外,请注意,您是第一次忘记自动发布.

Also, note you forgot your autorelease the first time through.

// TextCell
- (id) init {
  if ([super initWithStyle: UITableViewCellStyleDefault 
           reuseIdentifier: @"AlwaysTheSame"]) {
    self.myInternalStuff = [[[MyInternalStuff alloc] initWithFrame: CGRectZero] autorelease]; 
    // I don't know what size I am yet!
  }
  return self;
}

- (void) setHeight: (CGFloat) height {
  self.myInternalStuff.frame = CGRectMake(0, 0, 100, height);
  // I know what height I am now, so I can lay myself out!
}

这篇关于UITableView中的多个重用标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆