Objective C - 不正确的细胞数据

时间:2014-05-02 06:39:37

标签: ios objective-c uitableview

切入主题。 我有一个数组定义,变量I声明如下

 @interface KTCBLocationTableViewController () {
   NSArray *deserializedArray;
   int i;
 }

它存储从远程数据库检索的数据,当我使用以下代码查看内容时,该数据库最终包含以下数据

//This line is inside a for-loop
[[deserializedArray objectAtIndex:i] objectForKey:@"rname"];

上述代码的结果显示以下数据

Location A
Location B
Location C
Location D
Location E
Location F
Location G
Location H

我打算将这些数据放入UITableView的每个单元格中。使用以下代码。 此表附带可扩展行,以下代码仅显示每行的部分。

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

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

// Configure the cell...

if ([self tableView:tableView canCollapseSection:indexPath.section])
{
    if (!indexPath.row)
    {
        // first row        
            cell.textLabel.text = [[deserializedArray objectAtIndex:i] objectForKey:@"rname"]; // only top row showing
            i++;
            if ([expandedSections containsIndex:indexPath.section])
            {
                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
            }
            else
            {
                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
            }

    }
    else
    {
//The remaining of code are not given due to the irrelevance.

我发现在晃动后细胞正在发生变化,位置A可能会变成G或其他。

我看了一下这条线。

cell.textLabel.text = [[deserializedArray objectAtIndex:i] objectForKey:@"rname"]; // only top row showing
i++;

我觉得这就是问题所在。但是,我不知道我怎么能填充"数组中的所有数据到每行的单元格并使其保持一致。

2 个答案:

答案 0 :(得分:0)

使用' indexPath.row'。变化

cell.textLabel.text = [[deserializedArray objectAtIndex:i] objectForKey:@"rname"]; // only top row showing

cell.textLabel.text = [deserializedArray[indexPath.row] objectForKey:@"rname"]; // only top row showing

答案 1 :(得分:0)

嗯,你的问题很简单,但解决方案可能有点复杂。不按indexPath的顺序调用cellForRowAtIndexPath方法。我建议使用indexPath.row而不是i,即使它不容易

相关问题