Posts Tagged ‘Xcode’

25
Feb

自定义的 UITableCellView 需要注意的一个地方

   Posted by: 边城浪子    in iPhone & iPad

我在不少 iPhone 程序里面使用自定义的 UITableCellView,比如之前的 80’s Movie Quotes,tableView 表现得很流畅,但是在最近一个项目中,却出现了奇怪的现象。

我自定义了一个 UITableCellView,然后在代码中引用:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MainCell";
 
    MainCellViewController *cell = (MainCellViewController *)
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
	NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MainCellView"
                                                     owner:self
                                                   options:nil];
	cell = [nib objectAtIndex:0];
    }
 
    // Set up the cell...
    cell.titleLabel.text = [[content objectAtIndex:indexPath.row] title];
 
    return cell;
}

一切看起来正常,但是在滚动 tableView 的时候,却发现有很卡的感觉,开始以为是我的 Custom UITableCellView 用了太多透明的 png,后来发现不是这样。经过检查,发现问题出在 CellIdentifier 上。

我之前一直以为 CellIdentifier 只要能区分要重复使用的 UITableCellView 就可以了,在使用默认的 tableView 的时候确实表现如此,但是用在自定义 UITableCellView 的时候就不是这样了,感觉是元件没有被缓存,每次都要重新初始化似的。

后来,我为 UITableCellView 在 IB 里面指定了 Identifier,我的 tableView 滚动马上变得平滑了。

Identifier

这个定义应该可以在代码中实现,不过在 IB 中定义一下还是很方便的。

这个其实很简单,之所以记录一下,是要提醒自己,不能什么都想当然了。

Tags: , , , , , , , ,