如何在 UITableView 中加载更多单元格 SWIFT

How to load more cells in UITableView SWIFT

我有一个 UITableViewController(而不是 PFQueryTableViewController)来显示我的查询结果,我有一个存储文本的数组。由于查询会获取大量数据,因此我希望我的 tableView 在用户滚动到底部时加载更多结果。那里有很多解决方案,但它们要么在 JSON 中,要么在 ObjectiveC 中,它们对我来说真的很模糊,因为我只是一个初学者。

class queryResultsViewController: UITableViewController {

var texts = [String]()


override func viewDidLoad() {
    super.viewDidLoad()

    let query = PFQuery(className: "allPosts")

    query.whereKey("userId", equalTo: (PFUser.currentUser()?.objectId)!)
    query.orderByDescending("createdAt")

    query.findObjectsInBackgroundWithBlock { (posts, error) -> Void in

        if let posts = posts {

            self.texts.removeAll(keepCapacity: true)

            for post in posts {

                self.captionOne.append(post["text"] as! String)

                self.tableView.reloadData()
            }
        }
    }
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return texts.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! theCell

    cell.TextView.text = texts[indexPath.row]

    return cell
}

要检测用户何时滚动到 UITableView 底部,您可以实现 UIScrollView 委托方法 scrollViewDidScroll:

示例实现(从 转换为 Swift)

override func scrollViewDidScroll(scrollView: UIScrollView) {
    let offset = scrollView.contentOffset
    let bounds = scrollView.bounds
    let size = scrollView.contentSize
    let inset = scrollView.contentInset
    let y = CGFloat(offset.y + bounds.size.height - inset.bottom)
    let h = CGFloat(size.height)

    let reload_distance = CGFloat(10)
    if(y > (h + reload_distance)) {
        print("load more rows")
    }
}

触发时,您可以从解析中下载更多结果,将它们添加到 UITableView 的数据源并调用重新加载数据。

此外,查看您的代码,您可能需要调用 dispatch_async,因为您正在尝试更新后台块中的 UI,例如

dispatch_async(dispatch_get_main_queue()) { () -> Void in
        self.tableview.reloadData()
    }



编辑
从 Parse

加载更多结果
let query = PFQuery(className: "allPosts")

query.whereKey("userId", equalTo: (PFUser.currentUser()?.objectId)!)
query.orderByDescending("createdAt")

query.limit = 50 // or your choice of how many to download at a time (defaults to 100)
query.skip = 50 // This will skip the first 50 results and return the next limit after. If

query.makeRequest......



在您的完成处理程序中,确保将结果附加到整个数据源(在您的情况下 texts),并调用重新加载数据。

仅显示 20(例如)行并在屏幕底部的 UIToolBar 中添加一个 "Next" 按钮怎么样?当用户点击按钮时,您会显示第 21-40 行,等等。您还可以添加一个 "Previous" 按钮向后移动。

- (void)setUpToolbar
{
// add a toolbar with a prev and next button
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @""
                                                                         style: UIBarButtonItemStylePlain
                                                                        target: nil
                                                                        action: nil];

UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
                                                                              target: nil
                                                                              action: nil];

self.prevButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"Prev", nil)
                                                   style: UIBarButtonItemStylePlain
                                                  target: self
                                                  action: @selector(clickedPrevButton:)];

self.nextButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"Next", nil)
                                                   style: UIBarButtonItemStylePlain
                                                  target: self
                                                  action: @selector(clickedNextButton:)];

self.nextButton.enabled = NO;
self.prevButton.enabled = NO;
self.page = 1;

self.toolbarItems = @[self.prevButton, flexibleItem, self.nextButton];
}

- (void) clickedNextButton: (id) sender
{    
if ([self.nextButton.title isEqualToString: NSLocalizedString(@"More Results", nil)])
{
    self.offset += kSearchLimit;
    self.nextButton.title = NSLocalizedString(@"Next", nil);
    self.page += 1;

    [self searchDatabase];
}
else
{
    self.page += 1;

    if ((self.page - 1) * kEntriesToDisplay > self.searchResults.count)
    {
        self.nextButton.enabled = NO;
    }

    if (self.page * kEntriesToDisplay == self.searchResults.count)
    {
        self.nextButton.enabled = YES;
        self.nextButton.title = NSLocalizedString(@"More Results", nil);
    }

    self.prevButton.enabled = YES;

    [self updateSearchUI];        
}
}

- (void) clickedPrevButton: (id) sender
{
self.page -= 1;

if (self.page == 1)
    self.prevButton.enabled = NO;

self.nextButton.title = NSLocalizedString(@"Next", nil);
self.nextButton.enabled = YES;

[self updateSearchUI];
}

这是在 UITableView 中处理 load more 的正确方法。 为避免波动,滚动视图停止时调用以下方法。

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let scrollHeight = scrollView.frame.size.height

    let endScrolling = offsetY + scrollHeight

    if endScrolling >= scrollView.contentSize.height {
        //Load more logic
    }
}