gpt4 book ai didi

swift - 我将如何限制 tableview 中的项目数量并自动加载更多?

转载 作者:行者123 更新时间:2023-11-28 08:59:20 24 4
gpt4 key购买 nike

再次真正快速地感谢所有帮助过我的人。

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

我的应用程序中有一个搜索功能,它通过解析查找关键字并进行相应匹配。它现在工作正常,但随着列表变大,我假设加载时间可能有点长。它立即显示大约 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 个项目,然后在用户接近页面底部时加载后续项目。

最佳答案

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

粗略地说,在willDisplayCell:检查

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

并且在 cellForRowAtIndexPath: 中让最后一个表格单元格成为某种加载程序(直到您的模型得到更新并触发回调):

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

关于swift - 我将如何限制 tableview 中的项目数量并自动加载更多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32302055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com