【问题标题】:How would I limit the amount of items in a tableview and automatically load more?我将如何限制表格视图中的项目数量并自动加载更多?
【发布时间】:2015-11-24 22:41:28
【问题描述】:

再次非常感谢所有帮助过我的人。

我要处理的最后一件事如下。

我的应用程序中有一个搜索功能,它会通过解析来查找关键字并进行相应的匹配。它现在工作正常,但随着列表变大,我假设加载时间可能会有点多。它会立即显示大约 200 个文本结果,我想知道它在显示 2,000 个时是否会崩溃或滞后。

以下是我的搜索类的重要部分。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return searchResults.count
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! UITableViewCell
    //myCell.backgroundView = UIImageView(image: UIImage(named: "goodbg"))

    //myCell.contentView.backgroundColor =  UIImage(named: "nobg")
    if myCell.backgroundView == nil {
        myCell.backgroundView = UIImageView(frame: myCell.contentView.bounds)
        myCell.backgroundView?.contentMode = UIViewContentMode.ScaleToFill
        myCell.backgroundView?.clipsToBounds = true
        myCell.contentView.backgroundColor = UIColor.clearColor()

    }
    //myCell.layoutMargins = UIEdgeInsetsZero
    let backgroundImageView = myCell.backgroundView as! UIImageView
    backgroundImageView.image = UIImage(named: "nobg")
    myCell.textLabel?.text = searchResults[indexPath.row]
    myCell.textLabel?.font = UIFont(name: "Lato-bold", size: 17)
    myCell.textLabel?.textColor = UIColor(red: 126.0/255, green: 126.0/255, blue: 126.0/255, alpha: 1)
    myCell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
    myCell.textLabel?.numberOfLines = 0
    return myCell
}


func searchBarSearchButtonClicked(searchBar: UISearchBar)
{

    searchBar.resignFirstResponder()
    var textSearchQuery = PFQuery(className: "Red")

    textSearchQuery.whereKey("text", containsString: searchBar.text)


    var query = PFQuery.orQueryWithSubqueries([textSearchQuery])

    query.findObjectsInBackgroundWithBlock
        {
            (results: [AnyObject]?, error: NSError?) -> Void in

            if error != nil
            {
                //var myAlert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.self)
                //let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
                //myAlert.addAction(okAction)
                //self.presentViewController(myAlert, animated: true, completion: nil)
                return
            }

            if let objects = results as? [PFObject]
            {

                self.searchResults.removeAll(keepCapacity: false)
                for object in objects
                {
                    let searchText = object.objectForKey("text") as! String
                    self.searchResults.append(searchText)
                }

                dispatch_async(dispatch_get_main_queue())
                    {
                        self.myTable.reloadData()
                        self.searchBar.resignFirstResponder()
                }
            }
    }
}

我将如何做到这一点,所以可能首先加载 100 个项目,然后在用户靠近页面底部时加载后续项目。

【问题讨论】:

    标签: swift uitableview reload


    【解决方案1】:

    首先,您应该能够在范围内进行查询。然后,将self.searchResults 中的项目数与tableView(_: , willDisplayCell, forRowAtIndexPath:) 结合起来。也就是说,当行到达self.searchResults.count 时,单元格显示为“正在加载...”,同时触发下一个范围内的查询。

    粗略地说,在willDisplayCell:检查

    if notAlreadyFetching && self.searchResults.count == indexPath.row {
        // Fetch next range of 'searchResults'
    }
    

    cellForRowAtIndexPath: 中,最后一个表格单元格是某种加载器(直到您的模型得到更新并触发回调):

    if self.searchResults.count == indexPath.row {
        // Return a loader cell
    }
    

    【讨论】:

    • @JamesChen,我已经进一步阐述了。如果你用尽了你的模型,你就去获取更多的数据。同时,你给用户一些加载反馈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    相关资源
    最近更新 更多