表视图中的重复行
我知道有类似的问题,但它们对我不起作用!
我的行被重复,当用户返回任何带有表视图的选项卡时, UILabel 的文本变得越来越粗。
这是 cellForRowAtIndexPath 的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)
[tableView dequeueReusableCellWithIdentifier:CellClassName];
if (cell == nil)
{
NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
cell.subnavName.text = [array objectAtIndex:[indexPath section]];
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"subnavigation_off.png"]];
[cell setBackgroundView:background];
background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"subnavigation_on.png"]];
cell.selectedBackgroundView = background;
[background release];
return cell;
} 当用户从另一个选项卡返回此选项卡时,我该怎么做才能避免 UILabel 中的文本再次添加到我的单元格上?我读过一些关于使用标签的内容,但我找不到方法。
非常感谢!
I know there're similar questions like this, but they don't work for me!
I'm having my rows duplicated, and the text of the UILabel gets bolder and bolder when user comes back to any tab with table views.
Here's the code of cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)
[tableView dequeueReusableCellWithIdentifier:CellClassName];
if (cell == nil)
{
NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
cell.subnavName.text = [array objectAtIndex:[indexPath section]];
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"subnavigation_off.png"]];
[cell setBackgroundView:background];
background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"subnavigation_on.png"]];
cell.selectedBackgroundView = background;
[background release];
return cell;
}
What can I do to avoid the text from the UILabel to be added again on my cells when user comes back to this tab from another one? I read something about use the tag, but I can't find the way to do it.
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常经典的错误。
问题(就像这里关于 TableView 问题的常见问题一样)是您没有正确使用 TableViewCells 的重用机制。
您应该尝试使现有单元格出队(回收),如果它不返回单元格(表视图无法回收旧的(已分配但不再使用)单元格),则分配它并配置每个单元格所有单元格共有的属性(标签字体、添加子视图、更改颜色等)。
然后,在“if”之外 - 因此无论单元格是新分配的单元格还是已回收的单元格(来自先前分配但不再使用的单元格),在所有情况下 - 用特定内容填充单元格这取决于indexPath(文本,图像等)
阅读Apple文档中的表视图编程指南,并在StackOverflow上搜索也有很多关于TableViews的问题,它们都有相同的问题。
Soooooo classic mistake.
The problem (as quite always about TableView questions here) is that you don't use the reuse mechanism of the TableViewCells correctly.
You should try to dequeue (recycle) an existing cell, and if it does not return a cell (the tableview didn't manage to recycle an old -- already allocated but not used any more -- cell) then allocate it and configure every property that will be common to all your cells (label font, adding subviews, changing colors, etc).
Then, outside of the "if" -- so whether the cell is a newly allocated one or a cell that has been recycled (from a previously allocated but no more used cell), in all cases -- fill the cell with the specific content that depends on the indexPath (text, image, etc)
Read the Table View Programming Guide in Apple documentation, and search on StackOverflow too there are a lot of questions about TableViews that all have the same issue.