【发布时间】: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