问题
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:
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.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.
Check for unoptimized code. For example, if you're displaying a formatted date and are creating an instance of
NSDateFormatter
each timetableview:cellForRowAtIndexPath:
is called, this will really bog down your app.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