【问题标题】:UITableView scrollToRowAtIndexPath:atScrollPosition:animated: lag my app seriouslyUITableView scrollToRowAtIndexPath:atScrollPosition:animated: 严重滞后我的应用
【发布时间】:2016-07-26 03:51:38
【问题描述】:

我有一个表格视图。它将从服务器接收混乱的数据。每次 tableview 收到数据时,我都希望它在底部显示最新的数据。我打电话给scrollToRowAtIndexPath:atScrollPosition:animated:,但它落后于我的应用程序,当滚动到 tableview 底部时,我无法在屏幕上做任何其他事情。 这是我的主要代码

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

    DLtestCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary* dict = arr[indexPath.row];
    cell.myLabel.attributedText = dict[@"content"];
    [cell.myImageview sd_setImageWithURL:dict[@"imageUrl"] placeholderImage:[UIImage imageNamed:@"transparentPlaceHolder"]];    
    return cell;
}

这里是滚动到底部的主要代码

- (void)reloadTableView
{
    dispatch_async(dispatch_get_main_queue(), ^{

        NSIndexPath *lastRow = [NSIndexPath indexPathForRow:row inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[lastRow] withRowAnimation:UITableViewRowAnimationLeft];
    });
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}

【问题讨论】:

  • 尝试在主线程(performOnMainThread)方法中重新加载

标签: ios objective-c uitableview tableview reloaddata


【解决方案1】:

你应该使用scrollToRowAtIndexPath方法。

并在 viewWillAppear 中写下以下代码。

[theTableView reloadData];    
NSIndexPath* ip = [NSIndexPath indexPathForRow:rowNumber inSection:sectionNumberHere];
[theTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];

这里的 'rowNumber' 是您要滚动到的数据源中的行号。

或者您可以按如下方式计算“rowNumber”:

int lastRowNumber = [tableView numberOfRowsInSection:0] - 1; 

并将这个 lastRowNumber 传递给 indexPathForRow。

注意:numberOfRowsInSection 返回行数,而不是最后一行数(即行数 - 1)。

【讨论】:

  • 其实我就是这样做的,但是数据访问混乱导致我什么都不能操作
猜你喜欢
  • 1970-01-01
  • 2022-07-29
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 2023-03-06
  • 1970-01-01
  • 2013-06-16
相关资源
最近更新 更多