滚动后单元格变为空.(Xcode) [英] Cells become empty after scrolling. (Xcode)

查看:24
本文介绍了滚动后单元格变为空.(Xcode)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 tableview 有问题.当我滚动并且一个单元格从屏幕上消失时,它变成空白.我在情节提要中构建了一个原型单元格,其中包含两个标签和一个图像视图,它具有与我在代码中使用的标识符相同的标识符.我还为 customcell 构建了一个自定义类.这是 cellForRowAtIndexPath 中的代码:

I have a problem with my tableview. When I scroll and a cell disappears from the screen it becomes blank. I have built a prototype cell in the storyboard with two labels and one imageview, it have the same identifier that I use in my code. I also have built a custom class for the customcell. Here is the code in cellForRowAtIndexPath:

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

static NSString *CellIdentifier = @"Cell";
Scientist *currentScientist = [[xmlParser data] objectAtIndex:indexPath.row];

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

cell.self.cellName.text = currentScientist.self.name;
cell.self.cellSubject.text = currentScientist.self.subject;
cell.self.cellImage.image = currentScientist.self.image;

return cell;
}

我不知道您是否需要更多代码来帮助我.

I don't know if you need more code to help me.

推荐答案

我找到了一篇文章,详细介绍了您遇到的问题.我还建议打印出 Scientist 数据,以确保使用 objectAtIndex:indexPath.row 调用正确获取对象.

I found an article that details the problem you are encountering. I would also recommend printing out the Scientist data to ensure an object is being fetched correctly with the objectAtIndex:indexPath.row call.

从我在下面链接的文章中,我敢打赌您的 dequeueReusableCellWithIdentifier 是问题所在.解决此问题的一种快速方法是为每个单元格提供自己唯一的单元格标识符(只是为了测试理论).但是,解决此问题的合适方法是更改​​ cellForRowAtIndex 方法.

From the article I linked below, I would be willing to bet that your dequeueReusableCellWithIdentifier is the problem. One quick way you could solve this is by giving each cell its' own unique cell identifier (Just to test out the theory). However, it looks the appropriate way to solve this is by changing up your cellForRowAtIndex method.

来源

这篇关于滚动后单元格变为空.(Xcode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆