0

我有一个表格视图,它从当前包含 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 个单元格。

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

4

2 回答 2

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];
    }
}
于 2013-07-29T16:52:24.530 回答
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];
    }
}
于 2013-07-29T16:46:17.560 回答