2013-11-01 121 views
0

在我的故事板我有一个表视图。 我使用从JSON文件(在viewDidLoad中加载)加载的数据填充该表视图。UITableView重叠滚动

在我的UITableView中,我确实将“Prototype Cells”设置为1,因此我可以轻松选择一个附件。 我的原型单元有一个Seque到一个新的视图,需要显示所选项目的细节。

我填我的细胞编程方式使用这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *videoTableIdentifier = @"VideoCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:videoTableIdentifier]; 

    if (cell == nil) 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:videoTableIdentifier]; 

    UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(40,5, 240,20)]; 
    [cell addSubview:title]; 
    [title setText:[[videos objectAtIndex:indexPath.row] objectForKey:@"title"]]; 

    UILabel * detail = [[UILabel alloc] initWithFrame:CGRectMake(40,28, 100,10)]; 
    [cell addSubview:detail]; 
    [detail setText:[NSString stringWithFormat:@"Year: %@", [[videos objectAtIndex:indexPath.row] objectForKey:@"year"]]]; 

    [cell addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:[[videos objectAtIndex:indexPath.row] objectForKey:@"image"]]]]; 

    return cell; 
} 

当我开始滚动会发生以下情况: enter image description here

所以我开始寻找解决的办法,我发现我必须设置: dequeueReusableCellWithIdentifier:videoTableIdentifier为“无”。

虽然我这样做,这解决了我的问题,但是现在我原型单元配件和Seque都不见了,所以我不能导航到下一个视图了。

我找不到在互联网上的解决方案,所以我决定问这个。 如果有人可以帮忙,会很棒。

回答

6

你的问题是,每次你重新使用一个单元格时,你都在为它添加新的子视图,所以如果你有一个单元格有一些标签,并且你重复使用它并添加更多标签,它们都会出现重叠。

我建议你制作自己的带有标签的自定义UITableViewCell,每次只更改标签的值。

+0

这就是我的想法,因为我已经阅读了有关回收细胞的一些信息,但是我找不到如何解决这个问题。只有文本重叠,图像保持不变,即使这也是子视图?! – ronnyrr

+0

图像堆叠在另一个上面。如果它有帮助,请接受答案 –

+0

实际上,您可以将if(cell == nil)条件中的addSubview代码移入其中,并且无需创建UITableViewCell的子类就可以正常工作。但我认为创建自定义子类仍然是一个更好的方法 – Lukas

0
if (!title){ 
    title = [[UILabel alloc] initWithFrame:CGRectMake(40,5, 240,20)]; 
    [cell addSubview:title]; 
} 
[title setText:[[videos objectAtIndex:indexPath.row] objectForKey:@"title"]]; 

以前的答案作者是对的。使用你自己的UITableViewCell的子类可能会更好。
我没有看到任何内存版本。你在使用自动引用计数吗?