iOS UITableViewCell Optimization

旧巷老猫 提交于 2019-12-05 02:57:10

问题


In my app I have a UITableView that contains custom UITableViewCells. Scrolling is very sluggish and laggy, and I need to resolve this.

Each cell contains 5-6 labels, a few buttons, and an image.

I have a xib for the cell, where I place the buttons, labels, and image view, but I set all of the images in cellForRowAtIndexPath. To load the images I use [UIImage imageNamed:@""], which I read is better for optimization. I am also the reusing cells.

I also found that even if I comment out and do not set the images, the table still is laggy, with just the labels set with text.

Does anyone have any suggestions on how to have a rather complex table cell while still having good, smooth performance? Is having the custom cell as a xib hinder performance?


回答1:


  1. Make sure you really are reusing cells. Make sure that the cell identifier you are using is set in the XIB of the UITableViewCell in interface builder.

  2. Use less transparency in your subviews. If at all possible, go into your labels and make sure their background matches with that of the background of the cell.

  3. Check for unoptimized code. For example, if you're displaying a formatted date and are creating an instance of NSDateFormatter each time tableview:cellForRowAtIndexPath: is called, this will really bog down your app.

  4. Use Instruments with the Timing and Core Animation modules to find where the bulk of your time is spent. Ultimately, these can reveal where the true performance problem lies.




回答2:


Implement a custom UITableViewCell subclass and do your own drawing in drawRect: on one opaque UIView.

Doing it that way speeds up scrolling a lot.




回答3:


In IB set some properties to NO.

In scrollers, set "Horizontal scrolling" to NO. In Touch, set "Delay content touches" and "Cancellable content touches" to NO

It will enable smooth scrolling you'r table view



来源:https://stackoverflow.com/questions/9501304/ios-uitableviewcell-optimization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!