【发布时间】:2015-06-25 05:18:50
【问题描述】:
我在核心数据中有 100 行的表,在 tableView 中我加载过滤的 25 行。当我在应用程序中打开这个 UIViewController 需要将近 2 秒,我认为它非常慢。我在 iPhone 5 上测试我的应用程序。可能是我做错了什么吗? 我看到所有行的 cellForRowAtIndexPath 方法调用了 4 次:1-25,然后再调用 1-25 等。可以吗? 当我只加载 1 行时,它运行得很快。
class TipsViewController: UIViewController, UITableViewDelegate
{
@IBOutlet weak var tableView: UITableView!
var tips = [Tips]()
lazy var managedObjectContext : NSManagedObjectContext? =
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
if let managedObjectContext = appDelegate.managedObjectContext
{
return managedObjectContext
}
else {
return nil
}
}()
let defaults = NSUserDefaults.standardUserDefaults()
var languge:String!
override func viewDidLoad()
{
super.viewDidLoad()
languge = defaults.objectForKey("language") as! String
fetchLog()
}
func fetchLog()
{
let fetchRequest = NSFetchRequest(entityName: "Tips")
let sortDescriptor = NSSortDescriptor(key: "id", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate=NSPredicate(format: "language=%@", languge)
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [Tips]
{
tips = fetchResults
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int
{
return tips.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> TipsTableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cellt", forIndexPath: indexPath) as! TipsTableViewCell
cell.tipsTextView?.text = tips[indexPath.row].textShort
cell.tipsTextView.editable=false
cell.tipsTextView.userInteractionEnabled=false
cell.tipsTextView.textColor = UIColor(red: 0x7E/255, green: 0x7A/255, blue: 0x7F/255, alpha: 1.0)
cell.selectionStyle = UITableViewCellSelectionStyle.Default
return cell
}
func tableView(_tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
let cell = tableView(_tableView, cellForRowAtIndexPath: indexPath)
return cell.getHeight()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
if(segue.identifier == "showDetailx"){
var indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
var detailViewController:TipViewController = segue.destinationViewController as! TipViewController
detailViewController.tip = tips[indexPath.row]
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
self.performSegueWithIdentifier("showDetailx", sender: self)
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
if cell.respondsToSelector("setSeparatorInset:")
{
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:")
{
cell.preservesSuperviewLayoutMargins = false
}
if cell.respondsToSelector("setLayoutMargins:")
{
cell.layoutMargins = UIEdgeInsetsZero
}
}
}
【问题讨论】:
-
如果您不需要为单元格设置不同的高度,请删除 estimatedHeightForRowAtIndexPath 函数。这会影响性能。如果所有单元格的高度相同,则可以使用 tableview 属性设置单元格高度。并通过评论函数 willDisplayCell 来检查性能。