再次真正快速地感谢所有帮助过我的人。
我处理的最后一件事情如下。
我的应用程序中有一个搜索功能,它通过解析查找关键字并相应地进行匹配。它现在工作正常,但随着列表变大,我假设加载时间可能有点多。它立即显示大约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个项目,并在用户靠近页面底部时加载后续项目。
答案 0 :(得分:0)
首先,您应该能够在范围内查询。然后,将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
}