UITableview 的渲染问题
我在 iPhone 模拟器上的导航控制器中遇到了一个非常奇怪的 UITableview 问题。在显示的单元格中,只有一些单元格被正确渲染。它们都应该看起来相同,但大多数都缺少我设置的附件,滚动视图会更改哪个单元格具有附件,因此我怀疑这是某种单元格缓存发生,尽管每个单元格的内容都是正确的。我还设置了一个图像作为背景,这也只是偶尔显示,但我通过更改
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
(也只渲染带有背景的随机单元格)来解决这个问题,
cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
我现在需要解决附件仅显示在随机单元格上的问题。我尝试将代码从 cellForRowAtIndex 移动到 willDisplayCell 但没有任何区别。我输入了一个日志命令来确认它正在运行每一帧。
基本上它是一个表格视图(UITableViewCellStyleSubtitle),从服务器获取其信息。然后通过调用 reload 的委托方法进行更新。代码是:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", [NSString stringWithFormat:@"Setting colours for cell %i", indexPath.row]);
// Set cell background
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
// detailTableViewAccessory is a view containing an imageview in this view's nib
// i.e. nib <- view <- imageview <- image
cell.accessoryView = detailTableViewAccessory;
}
// Called by data fetching object when done
-(void)listDataTransferComplete:(ArticleListParser *)articleListParserObject
{
NSLog(@"Data parsed, reloading detail table");
self.currentTotalResultPages = (((articleListParserObject.currentArticleCount - 1) / 10) + 1);
self.detailTableDataSource = [articleListParserObject.returnedArray copy]; // make a local copy of the returned array
// Render table again with returned array data (neither of these 2 fixed it)
[self.detailTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
// [self.detailTableView reloadData];
// Re-enable necessary buttons (including table cells)
letUserSelectRow = TRUE;
[btnByName setEnabled:TRUE];
[btnByPrice setEnabled:TRUE];
// Remove please wait message
NSLog(@"Removing please wait view");
[pleaseWaitViewControllerObject.view removeFromSuperview];
}
我只包含我认为相关的代码,如果需要可以提供更多。我还无法在 iPhone 上测试它,所以我不知道这是否只是模拟器异常或我的代码中的错误。我总是从问题中得到很好的反馈,有什么想法吗?
I have a very strange problem with a UITableview within a navigation controller on the iPhone simulator. Of the cells displayed, only some are correctly rendered. They are all supposed to look the same but the majority are missing the accessory I've set, scrolling the view changes which cell has the accessory so I suspect it's some sort of cell caching happening, although the contents are correct for each cell. I also set an image as the background and that was also only displaying sporadically but I fixed that by changing
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
(which also only rendered a random cell with the background) to
cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
I now need to fix the problem with the accessory only showing on a random cell. I tried moving the code from cellForRowAtIndex to willDisplayCell but it made no difference. I put in a log command to confirm that it is running through each frame.
Basically it's a table view (UITableViewCellStyleSubtitle) that gets its info from a server & is then updated by a delegate method calling reload. Code is:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", [NSString stringWithFormat:@"Setting colours for cell %i", indexPath.row]);
// Set cell background
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
// detailTableViewAccessory is a view containing an imageview in this view's nib
// i.e. nib <- view <- imageview <- image
cell.accessoryView = detailTableViewAccessory;
}
// Called by data fetching object when done
-(void)listDataTransferComplete:(ArticleListParser *)articleListParserObject
{
NSLog(@"Data parsed, reloading detail table");
self.currentTotalResultPages = (((articleListParserObject.currentArticleCount - 1) / 10) + 1);
self.detailTableDataSource = [articleListParserObject.returnedArray copy]; // make a local copy of the returned array
// Render table again with returned array data (neither of these 2 fixed it)
[self.detailTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
// [self.detailTableView reloadData];
// Re-enable necessary buttons (including table cells)
letUserSelectRow = TRUE;
[btnByName setEnabled:TRUE];
[btnByPrice setEnabled:TRUE];
// Remove please wait message
NSLog(@"Removing please wait view");
[pleaseWaitViewControllerObject.view removeFromSuperview];
}
I only included code that I thought was relevant, can supply more if needed. I can't test it on an iPhone yet so I don't know if it's maybe just a simulator anomaly or a bug in my code. I've always gotten good feedback from questions, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了!我所认为的渲染问题实际上只是我的一个愚蠢问题(我盲目地遵循教程是正确的)。 IB 仅创建了一个被分配给一个单元格的附件视图实例。通过重新创建每个单元格创建的视图来修复它:
替换为
我还应该补充一点,我与单元格背景相关的问题已使用相同的方法修复。另外,您可以使用代替 addSubview 行
Resolved! What I thought was a rendering problem was actually just a stupidity problem on my part (serves me right for blindly following a tutorial). IB was only creating one instance of the accessory view which was being assigned to one cell. Fixed it by recreating the view on each cell creation:
replaced by
I should also add that the issue I had with the cell background was fixed with the same methodology. Also, in place of the addSubview line you could use