当我要求它滚动到底部时添加更多单元格时,TableView会删除单元格。为什么?

时间:2013-07-29 16:38:42

标签: ios objective-c tableview

我有一个表视图,它从一个当前包含45个项目的数组中填充自己。目标是它最初显示20,并在用户滚动到表视图的底部时递增显示20个项目。

以下是使用信息填充单元格的方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }

    cell.textLabel.text = [[listings objectAtIndex:indexPath.row] objectForKey:@"listingName"];

    return cell;
}

以下是确定表格视图长度的方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (listings.count < 20){
        return listings.count;
    }
    return tableViewSize; 
}

tableViewSize是一个实例变量,我用值20实例化。每当用户滚动到底部时,我使用以下方法将它递增20:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == (listings.count - 1)){
        tableViewSize += 20;
        if(tableViewSize > listings.count){
            tableViewSize = listings.count; 
        }
    }
    [tableView reloadData];
}

问题很奇怪。当我包含后一种方法tableView:willDisplayCell:forRowAtIndexPath:时,表视图只显示一个单元格(我无法分辨数组中的哪个对象,因为此时所有对象都是相同的。如果需要,我可以找到) 。如果我注释掉后一种方法,那么表格视图会正确显示20个单元格。

有谁知道为什么这个方法导致表视图以这种方式运行?

2 个答案:

答案 0 :(得分:0)

你让自己成为一个无限循环。

每次显示一个单元格时,您都会调用reloadData。在每次reloadData之后重新加载tableView并再次显示所有单元格。这将再次调用reloadData。之后,再次显示第一个单元格。等等。

尝试将对reloadData的调用放入if中,以增加tableView中的项目数。

像这样:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == (listings.count - 1)){
        tableViewSize += 20;
        if(tableViewSize > listings.count){
            tableViewSize = listings.count; 
        }
        [tableView reloadData];
    }
}

为了获得更好的用户体验,我使用了另一种重新加载新单元格的方法。现在,当滚动到达最后一个单元格时,它会停止。因此,用户必须滚动几次才能看到项目100.这样的事情可能会更好,因为它保持了滚动的动力:

// viewDidLoad
displayedCount = 20;
realCount = 200;   // according to your backing array



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return displayedCount;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y + scrollView.bounds.size.height > scrollView.contentSize.height * 0.75) {
        // reload when the user scrolled to 75% of the current cells. This way you can keep the momentum of the scroll and the table does not stop. 
        displayedCount += 20;
        if (displayedCount > realCount) {
            displayedCount = realCount;
        }
        [self.tableView reloadData];
    }
}

答案 1 :(得分:0)

我认为正在发生的是由于某些逻辑不正确而不断重新加载的tableview。像这样修改willDisplayCell并查看是否能解决问题:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == (tableViewSize - 1) && listings.count > tableViewSize) {
        tableViewSize += 20;
        if(tableViewSize > listings.count){
            tableViewSize = listings.count; 
        }
        [tableView reloadData];
    }
}