[英]UITableview Crashes on Scrolling
i'm now stuck on this problem for days and i haven't been able to figure it out..... I created my project from the navigation based template and it automatically generated a tableview as well. 我现在已经在这个问题上待了好几天了,而且我一直无法弄清楚.....我从基于导航的模板创建了我的项目,它也自动生成了一个表格视图。 I then added some sections and some rows and tried to fill the rows of the table with simple strings. 然后,我添加了一些部分和一些行,并尝试用简单的字符串填充表的行。 It all works fine until there are a certain amount of sections and rows the table reaches a certain length. 一切正常,直到表中有一定数量的节和行达到一定长度为止。 The content of the first row then also appears in the last two rows and i have no idea why... Please HELP!!! 然后第一行的内容也出现在最后两行中,我不知道为什么...请帮助! Here's my code: 这是我的代码:
#import "RootViewController.h"
@implementation RootViewController
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release anything that can be recreated in viewDidLoad or on demand.
// e.g. self.myOutlet = nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"test";
}
}
return cell;
}
- (void)dealloc {
[super dealloc];
}
@end
UPDATE: 更新:
Ok so i think i've got it but i have no idea what was actually happening it was just a coincidence but when i changed the line: 好的,所以我认为我已经掌握了,但是我不知道实际发生了什么,这只是一个巧合,但是当我改变这行代码时:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
to: 至:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
it worked... 有效...
Can somebody pls explain this to me???? 有人可以向我解释一下吗???? ;-) ;-)
As you have already noticed, this is due to the way reusable cells work. 正如您已经注意到的,这是由于可重用单元的工作方式所致。
However, you really should reuse your cells and always reset their content in tableView: cellForRowAtIndexPath:
. 但是,您确实应该重用单元格,并始终在tableView: cellForRowAtIndexPath:
重置其内容。
So for example you can do this: 因此,例如,您可以执行以下操作:
cell.textLabel.text = @"";
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"test";
}
}
But normally you would have proper content for each cell. 但是通常每个单元格都有适当的内容。
EDIT: You should really read the Table View Programming Guide , but here's the important excerpt for this question: 编辑:您应该真正阅读《 表视图编程指南》 ,但这是此问题的重要摘录:
The table view's data source implementation of
tableView:cellForRowAtIndexPath:
should always reset all content when reusing a cell. 重新使用单元格时,tableView:cellForRowAtIndexPath:
的表视图的数据源实现应始终重置所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.