当用户滚动时,在tableview底部显示微调器的好方法是什么,但是tableview单元格加载缓慢?

时间:2017-07-28 04:54:41

标签: ios swift performance uitableview

我有一个显示一些内容的UITableView。当用户向上滚动时,下面的单元格不会立即加载。这会在表格的底部创建一个空白区域。我想在白色空间中显示一个微调器,并在内容完成加载后消失,但我不知道该如何去做。什么是在swift中实现这样的东西的好方法?

我是编码和iOS的新手,如果问题含糊不清或答案很明显,请原谅我。

示例屏幕截图:

sample image for desired behavior

2 个答案:

答案 0 :(得分:4)

我认为这对你有帮助..

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let lastSectionIndex = tableView.numberOfSections - 1
    let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
    if indexPath.section ==  lastSectionIndex && indexPath.row == lastRowIndex {
        // print("this is the last cell")
        let spinner = UIActivityIndicatorView(activityIndicatorStyle: .red)
        spinner.startAnimating()
        spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))

        self.tableview.tableFooterView = spinner
        self.tableview.tableFooterView?.isHidden = false
    }
}
数据加载时应隐藏

tableFooterView。 当上述功能不起作用时,您可以选择此link.

答案 1 :(得分:0)

在viewDidLoad中将UIActivityIndi​​catorView设置为UITableView的footerView。

self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.indicator.hidesWhenStopped = YES;
self.indicator.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44);

self.tableView.tableFooterView = self.indicator;

当表视图将要显示单元格的最后一行并且您有更多数据要加载时,然后加载更多数据。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == self.arrList.count-1 && self.hasMoreList == YES) {
        [self loadData];
    }
}

仅在加载之前启动动画指示器,并在加载数据后停止对其进行动画处理。

- (void)loadData {

    [self.indicator startAnimating];

    // load data and set hasMoreData here ... 

    [self.indicator stopAnimating];
}