- Use an opaque view.
Opaque views can greatly increase the speed of rendering. Therefore, if it is not necessary, you can set the Opaque property of the table cell and its child views to Yes (the default value).
The exceptions include the background color, which should have an alpha value of 1 (for example, do not use Clearcolor), an image should have an alpha value of 1, or be opaque when drawing.
- Do not create unnecessary table cells repeatedly.
As I said earlier, UITableView only needs a screen of UITableViewCell objects. So when the cell is not visible, you can cache it and continue to use it when you need it.
UITableView also provides this mechanism simply by setting a identifier:
Static NSString *cellidentifier = @ "xxx";
UITableViewCell *cell = [tableViewdequeuereusablecellwithidentifier:cellidentifier];
if (cell = = Nil) {
cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier] Autorelease];
}
It is worth mentioning that when the cell is reused, the content drawn inside it is not automatically cleared, so you may need to call the Setneedsdisplayinrect: or Setneedsdisplay method. In addition, when adding a table cell, if you do not need an animation effect, it is best not to use the Insertrowsatindexpaths:withrowanimation: method, but instead call the Reloaddata method directly. Because the former invokes the Tableview:cellforrowatindexpath: method on all indexpaths, even if the cell does not need to be displayed (not known as a bug), it is possible to create a large number of extra cells. errata: Just test this on the simulator, there is no such bug when debugging the real machine.
3. Reduce the number of views.
UITableViewCell includes views such as Textlabel, Detailtextlabel, and ImageView, and you can customize some of them in its contentview. However, view is a large object, and creating it consumes more resources and also affects rendering performance. If your table cell contains pictures and a large number, using the default UITableViewCell can have a significant impact on performance. Oddly, using a custom view, rather than a predefined view, is significantly faster. The best solution, of course, is to inherit UITableViewCell and draw it on its own drawrect:
-(void) DrawRect: (cgrect) rect {
if (image) {
[Image Drawatpoint:imagepoint];
self.image =nil;
} Else {
[PlaceHolder Drawatpoint:imagepoint];
}
[Text Drawinrect:textrect Withfont:font linebreakmode:uilinebreakmodetailtruncation];
}
However, you will find that the cell becomes blue when the row is selected, and the contents are blocked. The simplest way is to set the cell's Selectionstyle property to Uitableviewcellselectionstylenone so it won't be highlighted. You can also create a calayer, draw the content onto a layer, and then call Addsublayer: method on the cell's contentview.layer. In this example, the layer does not significantly affect performance, but if the layer is transparent, or has rounded corners, deformations and other effects, it will affect the drawing speed. Workaround refer to the pre-rendered image later.
4. Do not do extra drawing work.
When implementing DrawRect:, its rect parameter is the area that needs to be drawn, which is not required to be drawn outside this area. In the example above, you can use Cgrectintersectsrect, cgrectintersection, or cgrectcontainsrect to determine if you need to draw an image and text, and then call the drawing method.
5. Pre-render the image.
You will find that even if the above points are achieved, there will still be a brief pause when the new image appears. The solution is to draw it once in the bitmap context, export it to a UIImage object, and then draw it to the screen, with detailed instructions for accelerating the image display of the iOS device using pre-rendering.
6. Do not block the main thread.
When you do, your table view should be smooth enough to scroll, but you can still make users feel uncomfortable. The common phenomenon is that when updating data, the entire interface is stuck and does not respond to user requests at all. This behavior occurs because the main thread executes a long-time function or method that cannot draw the screen and respond to user requests until it finishes executing. The most common of these is the network request, which usually takes a few seconds, and you should not let the user wait that long. The workaround is to use multi-threading to let the child threads execute the functions or methods. There is also a knowledge that, when the number of download threads exceeds 2 o'clock, it can significantly affect the performance of the main thread. So when using ASIHTTPRequest, you can use a nsoperationqueue to maintain the download request and set its maxconcurrentoperationcount to 2. Nsurlrequest can be implemented with GCD, or using Nsurlconnection's Setdelegatequeue: method. Of course, you can also increase the number of download threads to speed up download times when you don't need to respond to user requests:
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate {
if (!decelerate) {
Queue.maxconcurrentoperationcount =5;
}
}
-(void) scrollviewdidenddecelerating: (Uiscrollview *) ScrollView {
Queue.maxconcurrentoperationcount =5;
}
-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView {
Queue.maxconcurrentoperationcount =2;
}
In addition, automatic loading of updated data is also friendly to users, which reduces the time users wait to download. For example, if you load 50 messages at a time, you can load more information when you scroll to the bottom 10th:
-(void) TableView: (uitableview *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: ( Nsindexpath *) indexpath {
if (Count-indexpath.row <&&!updating) {
updating = YES;
[Selfupdate];
}
}
After the update method obtains the result, set updating to no
It is also important to note that when the picture is downloaded, if the cell is visible, you also need to update the image:
Nsarray *indexpaths = [self.tableview indexpathsforvisiblerows];
For (Nsindexpath *visibleindexpath inindexpaths) {
if (Indexpath = = Visibleindexpath) {
Mytableviewcell *cell = (Mytableviewcell *) [Self.tableview Cellforrowatindexpath:indexpath];
Cell.image = image;
[Cell Setneedsdisplayinrect:imagerect];
Break ;
}
}
Ios-uitableview Performance Optimization