UITableView单元格全部重复第一个单元格 [英] UITableView Cells All Repeat 1st Cell

查看:63
本文介绍了UITableView单元格全部重复第一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView可以正常工作,直到向其添加部分为止-现在第一个单元格中的数据在所有其他单元格中重复.当我删除下面的节线时,一切恢复正常.

I have a UITableView that was working fine until I added sections to it - now the data from the first cell repeats throughout all the other cells. When I remove the sections line below, everything goes back to normal.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger sections = self.objects.count;

    return sections;
}

关于这里可能发生什么的任何想法?顺便说一下,我的数据源是Core Data.

Any ideas on what could be going on here? My data source is Core Data, by the way.

编辑

对于 numberOfRows ,我返回1.

这是我实现单元格的方式:

Here's how I'm implementing the cells:

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

Item *p = [[[ItemStore defaultStore] allItems]
                                objectAtIndex:[indexPath row]];

    ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];

    [cell setController:self];
    [cell setTableView:tableView];

    [[cell nameLabel] setText:[p itemName]];
    [[cell serialNumberLabel] setText:[p serialNumber]];
    [[cell valueLabel] setText:[NSString stringWithFormat:@"$%d", [p valueInDollars]]];

    [[cell thumbnailView] setImage:[p thumbnail]];


return cell;
}

推荐答案

您可能正在使用

cell.textlabel.text = [self.objects objectAtIndex: indexPath.row];

在cellForRowAtIndexPath方法中.

in your cellForRowAtIndexPath method.

这将在具有一个部分的表格视图中工作,因为行号每次都会增加.但是,如果您有多个部分,则每个部分的第一行的行号都重置为0.因此,indexPath.row在cellforRowAtIndexPath方法中将始终返回0,因此每次都将获得第一个对象.您将必须检查您所在的节,甚至将节号用作从数组中获取正确对象的一种方式.

This would work in a table view with one section, as the row number will increase each time. However, if you have multiple sections, the row number is reset back to 0 for the first row in each section. Therefore, indexPath.row will always return 0 in the cellforRowAtIndexPath method, hence you are getting the first object each time. You will have to do a check for which section you are in, or even use the section number as a way of getting the correct object from the array.

查看您发布的代码,这就是正在发生的事情!研究确定您所在的部分,并适当地填写它.

Looking at the code you've posted, this is exactly what's happening! Look into identifying which section you're in, and filling it appropriately.

这篇关于UITableView单元格全部重复第一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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