首先,注册UITableViewCell以供重用,如下所示
UINib *cellLoader=[UINib nibWithNibName:@"GroupCell_iPhone" bundle:nil];
[self.tableView registerNib:cellLoader forCellReuseIdentifier:@"GroupCell"];
然后在cellForRowAtIndexPath委托中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
该信元已出队。
GroupCell_iPhone *cell=(GroupCell_iPhone*)[self.tableView dequeueReusableCellWithIdentifier:@"GroupCell"];
然后,根据一系列条件动态创建一系列UILabel和其他对象,并将其添加到单元格,如下所示
[cell.contentView addSubview:noActivityLabel];
当第二个和随后的信元出列并且看起来具有来自第一个出列的信元的动态添加的对象时,问题就出现了。最后,每个细胞都是不同的。出列的单元是指向UITableViewCell的一个实例的“指针”吗?为什么这些随后出列的信元具有第一个信元的内容?
如果是这样的话,用动态/变化的内容创建单元格的最佳方法是什么?是否应该每次都创建单元格的新实例?出列的信元可以“克隆”吗?
说明:表格中的所有单元格都以基本布局开始,但随后会根据与该单元格相关的一些数据向每个单元格添加独特的内容。因此,表中的每个单元格都是唯一的,即存在不同的子视图(UILable、UIImageView等)。在牢房里。
发布于 2012-10-29 23:59:47
为什么这些随后出列的单元具有来自第一个单元的内容?
与其每次重新创建新的单元格,不如重用已经创建的不再显示的单元格。这就是将单元出列时tableView实际执行的操作:如果可以,它会给出一些未显示的单元。
假设您需要显示一个包含1000个单元格的表视图,而tableview实际上一次只显示6个单元格。它将重用单元堆栈,而不是创建1000个单元,这样就不需要重新创建新的单元。
出列的单元是指向UITableViewCell实例的“指针”吗?为什么这些随后出列的单元具有来自第一个单元的内容?
因为它是相同的指针对象,所以它将具有与第一次显示时相同的内容。然后,您需要更新其数据。
是否应该每次都创建单元格的新实例?出列的信元可以“克隆”吗?
在以下情况下,您只需创建新的单元格
[self.tableView dequeueReusableCellWithIdentifier:@"GroupCell"]
不返回任何内容。下面是一个简单的例子:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// get your cell from the stack
GroupCell_iPhone *cell=(GroupCell_iPhone*)[self.tableView dequeueReusableCellWithIdentifier:@"GroupCell"];
// or create new one if nil. Note: you must give it the same identifier than the
// one you are using for the dequeue. Or tableview will not be able to return you anything from the dequeing.
if(!cell)
cell = [GroupCell_iPhone newWithIdentifier: @"GroupCell"] + autorelease];
**更新**
// here you are sure to have a valid cell, so you can display content according to the indexPath
// display: use a controller who will update your cell change
// note, the first method clean what was inside your cell. You can keep the subview and reuse them the same way as tableview do.
[myDisplayClassCellController emptyCellSubview: cell];
// here you add all the display logic you want into your cell view. Note, you need indexPath
// because you need to display depending of the stack.
[myDisplayClassCellController decorateCellWithCustomViews: cell atIndexPath: indexPath];
**结束更新**
// then you can return back the cell to the tableView
return cell;
}
发布于 2012-10-30 15:42:24
我不确定这是不是最好的方法,但我已经做到了以下几点:
创建了一个带有几个子类的CustomCellClass : CustomCellType1,CustomCellType2自定义CellType3等,其中所有的CustomCellSubclasses都使用同一模型的不同部分来显示信息。
然后,我可以向CustomCellClass添加一个设置一些基本属性的prePareDataForCell函数。如果CustomCellSubclass需要它们,它就有它们,如果不需要,事情就会照常进行(我想,这是我不确定它是否是好的实践的一部分)。
然后我可以像这样做一些伪代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCellClass *returnCell = (CustomCellClass*)[tableView cellForRowAtIndexPath:indexPath];
MyObject *obj = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([returnCell.reuseIdentifier isEqualToString:CustomCellType1) {
CustomCellType1 *cell = (CustomCellType1*)[tableView dequeueReusableCellWithIdentifier:CustomCellType1];
if (!cell) {
cell = [[CustomCellType1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellType1];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellType1" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
// This is a basic cell doesn't need anything special done with the object model
returnCell = cell;
} else if ([[returnCell reuseIdentifier] isEqualToString:CustomCellType2]) {
CustomCellType2 *cell = (CustomCellType2 *)[tableView dequeueReusableCellWithIdentifier:CustomCellType2];
if (!cell) {
cell = [[CustomCellType2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellType2];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellType2" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
// Do stuff with obj data only needed for this type of cell, perhaps modify the cells subviews
[cell preConfigureCustomCellType2:obj];
returnCell = cell;
} else {
CustomCellType3 *cell = (CustomCellType3*)[tableView dequeueReusableCellWithIdentifier:StandardMailCell];
if (!cell) {
cell = [[CustomCellType3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:StandardMailCell];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellType3" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
// Do stuff with obj data only needed for this type of cell perhaps modify the same subviews in a different way for this type of cell
[cell preConfigureCustomCellType3:mail];
returnCell = cell;
}
if (![tableView isDragging] && !tableView.decelerating) {
// I used this to only run an expensive operation after the the customer flicks and the table comes to a rest.
}
return returnCell;
}
https://stackoverflow.com/questions/13124539
复制相似问题