-1

这可能与以前的问题重复,但我无法弄清楚为什么我的记录会重复。我知道该表试图回收行。似乎该表正在为插入到表中的每条数据添加更多单元格。

例如,对于 2 条记录,我得到 4 行。对于 3 条记录,我得到 9 行。

这里的代码:

UITable 委托的东西:

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return letters.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return letters.count;
}

- (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];
    }

    printf("%li", (long)indexPath.row);

    [cell textLabel].text = [letters objectAtIndex:indexPath.row];

    return cell;
}

我是否必须先检查数据是否已经显示?

4

1 回答 1

1

找到了答案。非常愚蠢但很容易错过。我已将其设置为:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return letters.count;
}

应设置为 1,因为只有 1 个部分要显示。我会把这个留在这里,以防有人犯同样的愚蠢错误!

IE

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
于 2013-09-29T06:54:35.817 回答