A previous article about the use of UITableView, UITableView in the status of iOS and the status of the ListView in Android is basically comparable, there are many articles on the optimization of the ListView online. UITableView Apple itself has optimized its functionality, no matter how much data you have, load the current page of data each time, so as not to cause unnecessary memory consumption. A very common optimization is to use the I dentifier, which is a unique indicator, to place objects that are not used inthe page in the cache pool if new objects appear to be removed from the cache pool.
Page Layout
Page layout or the same as the previous article, a tableview:
but need extra work when this time is the clothing information, through the frame to modify the price of clothing, need to create a new clothing class, to achieve uialertviewdelegate Protocol, the declaration in the header file:
@interface Viewcontroller:uiviewcontroller <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate> @property (Weak, nonatomic) Iboutlet UITableView *tableview; @end
Definition of clothing Category:
@interface Dress:nsobject@property (strong,nonatomic) nsstring *dressname; @property (strong,nonatomic) NSString * Dressdetial, @property (strong,nonatomic) nsstring *dressimage; @end
Optimization and Implementation
Before the optimization to implement some of the necessary functions, as well as some methods of use, the previous article is only a part of it, in order to better understand, you can first look at the effect of the implementation:
Define an array of stored data:
@interface Viewcontroller () { nsarray *imagearr; Nsarray *dressarr; Nsmutablearray *dresslist;} @end
Initialize data:
-(void) viewdidload { [super viewdidload]; Do any additional setup after loading the view. Imagearr=[[nsarray alloc]initwithobjects:@ "Dress1.jpeg", @ "dress2.jpg", @ "dress3.jpg", nil]; Dressarr=[[nsarray alloc]initwithobjects:@ "Seven Wolves", @ "Sen ma", @ "Jack Jones", @ "The United States", @ "to pure", @ "Jeanswest", @ "Hainan home", Nil]; Dresslist=[nsmutablearray arraywithcapacity:30]; for (Nsinteger i=0; i<30; i++) { Dress *dress=[[dress alloc]init]; Nsinteger Imagerandom=arc4random_uniform (3); Nsinteger Dressrandom=arc4random_uniform (7); Nsinteger Price=arc4random_uniform (+100); Dress.dressimage=imagearr[imagerandom]; Dress.dressname=dressarr[dressrandom]; Dress.dressdetial=[nsstring stringwithformat:@ "Promotional Price:%ld", (long) prices]; [Dresslist addobject:dress];} }
To set the number of rows:
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ return [dresslist Count];}
Set Grouping:
-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{ return 1;}
Set the cell contents, dequeuereusablecellwithidentifier the cells that can be reused by reuseidentifier settingthe cells that are reused:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ static NSString *[email protected] "Cachecell"; Generate a unique tag UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:flag]; if (cell==nil) { //Set the style that needs to show details Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:flag]; } Dress *dress=dresslist[indexpath.row]; UIImage *image=[uiimage ImageNamed:dress.dressImage]; [Cell.imageview Setimage:image]; [Cell.imageview setframe:cgrectmake (0, 0, +)]; [Cell.textlabel SetText:dress.dressName]; Set display small arrow [cell setaccessorytype:uitableviewcellaccessorydisclosureindicator]; Set tag// [cell setTag:indexPath.row]; [Cell.detailtextlabel setText:dress.dressDetial]; NSLog (@ "Get Updated lines:%ld", indexpath.row); return cell;}
box settings after the row is selected:
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{ Uialertview * Alterview=[[uialertview alloc] initwithtitle:@ "Clothing Price:" Message:nil delegate:self cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "Modified", nil]; [Alterview Setalertviewstyle:uialertviewstyleplaintextinput]; Selected cell UITableViewCell *cell=[tableview Cellforrowatindexpath:indexpath]; Uitextfield *textfield=[alterview textfieldatindex:0]; Set the modified clothing information [TextField SetText:cell.detailTextLabel.text]; [TextField SetTag:indexPath.row]; [Alterview show];}
Click events in the Uialterview:
-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) buttonindex{ if (buttonindex==1) { Uitextfield *textfield=[alertview textfieldatindex:0]; Nsarray *selectedrow=[self.tableview indexpathsforselectedrows]; Nsindexpath *indexpath=selectedrow[0]; Dress *dress=dresslist[indexpath.row]; Dress.dressdetial=textfield.text; NSLog (@ "%@---%ld", textfield.text, (long) indexpath.row); [Self.tableview Reloadrowsatindexpaths:selectedrow withrowanimation:uitableviewrowanimationright]; }
Set line height:
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{ return 50;}
About the optimization of uitable:
1. The most common is not to repeat the generation of cells, very common, very practical;
2. Using an opaque view can improve rendering speed, and the default TableCell background in Xcode is opaque;
3. If it is necessary to reduce the entries in the view, this article set Textlabel,detialtextlabel,imageview,accessorytype;
4. Do not update the item as a whole, update the selected can, suggest reloadrowsatindexpaths, instead of using Reloaddata;
iOS Development-uitableview Table optimization