我创建了在 tableview 中插入 12 行的示例应用程序。并插入得很好。当插入我的行后,我不希望在滚动 tableview 期间对文本进行任何更改。所以我检查了 indexpath 行的值为 UITableViewCell ,如果它有值意味着返回否则我们创建了新的 UITableViewCell。

我的示例代码如下

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;

}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {
NSLog(@"GlobalCount = %d",GlobalCount);
return GlobalCount;

}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *obj = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
if(obj!=nil)
{
    NSLog(@"cell indexpath row = %d",indexPath.row);
    NSLog(@"cell text = %d",obj.textLabel.text);
    return obj;
}

else {
    NSLog(@"obj==nil");
}


static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

if([DataTable count]>0)
    cell.textLabel.text = [DataTable objectAtIndex:0];
// Configure the cell.
return cell;

}

我的目标是,我不想为已经创建的索引路径行(单元格)更新任何文本(或 UITableViewCell)。所以我可以在 cellForRowAtIndexPath 中检查这个,但它总是返回 nil。如果我遗漏了什么?

我用这条线检查过
UITableViewCell *obj = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
if(obj!=nil)

请帮帮我?

提前致谢......

最佳答案

让表格 View 决定是否需要更新单元格。只需删除代码的所有第一部分,一切都会顺利运行。

不要犹豫,阅读 UITableView 文档。

关于iphone - cellForRowAtIndexPath 总是返回 nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3812369/

10-14 21:55
查看更多