【发布时间】:2012-08-07 00:54:23
【问题描述】:
您好,我是 IOS 开发新手,我创建了一个 UITableView,它使用在 nib 中创建的自定义单元格。下面是我的ViewController 中的代码,它正在加载单元格,但是如果我上下滚动 3 次,应用程序就会崩溃,因为我认为我没有正确地重用单元格。我四处搜索,但我发现的大部分代码/解决方案似乎已经过时了。我的代码在下面,任何帮助都非常感谢!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
cell.TITLE.text = [NSString stringWithFormat:@"\"%@\"", [TITLE objectAtIndex:indexPath.row]];
cell.desc.text = [desc objectAtIndex:indexPath.row];
cell.votes.text = [votes objectAtIndex:indexPath.row];
return cell;
}
【问题讨论】:
标签: ios uitableview nib recycle