iOS Development Learning Note 028-uitableview Single-group data display code optimization

Source: Internet
Author: User

1, performance optimization (add hundreds of cell to view) 2, how to implement the selection of a row, change the cell at the far right to display the checkmark button

1, if the table and hundreds of data, the system will automatically load the data displayed on the interface, one by one load

Add 100 data to UITableView

1      for(inti =0; I < -; i + +)2     {3NSString *icon = [NSString stringWithFormat:@"00%d.png", Arc4random_uniform (8) +1];4NSString *name = [NSString stringWithFormat:@"section%d", I];5NSString *desc = [NSString stringWithFormat:@"description of line%d", I];6Shop *tmp =[Shop Shopwithicon:icon andname:name Anddesc:desc];7 [_shops addobject:tmp];8         9}

When the slide screen is displayed, only the data displayed in the current screen is loaded.

1 //Set Line Contents2 //every time a cell enters the field of view, it calls3-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath4 {5UITableViewCell * cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@"C1"];6Cell.textLabel.text = [NSString stringWithFormat:@"Line%ld", Indexpath.row];7NSLog (@"%p, page%ld data", Cell,indexpath.row);8 9     returncell;Ten}

Only three cells are displayed in the interface, such as sliding down, loading a new cell at more than three each time, swiping up to reload the cell, and re-requesting memory each time.

If you want to avoid this situation you can use the cache pool, which is the UITableViewCell method dequeuereusablecellwithidentifier

1 //Set Line Contents2 //every time a cell enters the field of view, it calls3-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath4 {5      //Select the recyclable cell from the cache pool, specify the identity C1, and you will find the structure of the cell6UITableViewCell *cell = [TableViewDequeuereusablecellwithidentifier:@"C1"];7     //if the cache pool does not have8     if(Cell = =Nil)9     {Tencell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@"C1"];//Setting the identity C1 One     } A     //UITableViewCell * cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@ "C1 "]; -Cell.textLabel.text = [NSString stringWithFormat:@"Line%ld", Indexpath.row]; -NSLog (@"%p, page%ld data", Cell,indexpath.row); the  -     returncell; -}

See Running results

The interface starts to show three cells, and when you swipe down, there's a transition to create a new cell, but then you'll use the cell that already exists, and the cell created using line No. 0 starting with line fourth.

Source code: HTTP://PAN.BAIDU.COM/S/1I3QYAJJ

2, how to implement the selection of a row, change the cell to the right of the display of the checkmark button

Select a row and uncheck a row

1. Select a line to execute the method

1 //Select a row to execute2- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath3 {4NSLog (@"selected");5     //color becomes darker when selected6     //display a checkmark icon on the far right7     //1. Get the selected row8Shop *s =_shops[indexpath.row];9     //2. Modify the selected row's data to add the selected cell to the array to be deletedTen     if([_deleteshops containsobject:s])//if it already exists, click again to uncheck the button One     { A [_deleteshops removeobject:s]; -     } -     Else    //Otherwise, add the array you want to delete the     { - [_deleteshops addobject:s]; -     } -     //3, update the data, update the data is to reset the content of a row + [TableView Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; -      +}

2. Uncheck a row

1 // uncheck a row to execute 2 -(void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) Indexpath3{4     NSLog (@ "deselected"); 5 }

3. Re-set the contents of the selected row

1 //Set Line Contents2 //every time a cell enters the field of view, it calls3-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath4 {5     StaticNSString *id =@"C1";6     //Select the recyclable cell from the cache pool, specify the identity C1, and you will find the structure of the cell7UITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:id];8     //if the cache pool does not have9     if(Cell = =Nil)Ten     { Onecell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id];//Setting the identity C1 A     } -     //UITableViewCell * cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@ "C1 "]; -     //update data to interface theShop *s =_shops[indexpath.row]; -Cell.textLabel.text =S.name; -Cell.imageView.image =[UIImage Imagenamed:s.icon];; -Cell.detailTextLabel.text =S.desc; +     //Show right-most button -     if([_deleteshops containsobject:s])//determine if the cell is already selected, is the word set icon +     { ACell.accessorytype =Uitableviewcellaccessorycheckmark; at     } -     Else    //Otherwise, it doesn't show anything . -     { -Cell.accessorytype =Uitableviewcellaccessorynone; -     } -      in    //NSLog (@ "%p, section%ld data", cell,indexpath.row); -      to     returncell; +}

The code uses a new array to hold the selected row _deleteshops, and to make a decision about updating the data.

4. Loading pictures and text using a plist file

1- (void) Viewdidload2 {3 [Super Viewdidload];4     //additional setup after loading the view, typically from a nib.5     6     //Read *.plist file7     //1. Get the full path8NSString *path = [[NSBundle mainbundle] Pathforresource:@"Shops"OfType:@"plist"];9     //2. Reading data to an arrayTenNsarray *array =[Nsarray Arraywithcontentsoffile:path]; One     //initializing an array A_shops =[Nsmutablearray array]; -_deleteshops =[Nsmutablearray array]; -     //NSLog (@ "%d", array.count); the     //adding data to the interface -      for(Nsdictionary *arrinchArray) -     { -         //1. Create Shop +Shop *s =[Shop Shopwithdict:arr]; -         //2. Adding to an array + [_shops addobject:s]; A     } at      -}

5, the shop model has made some other modifications, adding or subtracting a class method and an object method for returning the shop object

1- (ID) Initwithdict: (Nsdictionary *) Dict2 {3Shop *shop =[Shop alloc] init];4Shop.icon = dict[@"icon"];5Shop.name = dict[@"name"];6Shop.desc = dict[@"desc"];7     returnShop ;8 }9+ (ID) Shopwithdict: (Nsdictionary *) DictTen { One     return[[Self alloc] initwithdict:dict]; A}

Effect

Source code: Http://pan.baidu.com/s/1mgxKgMO

iOS Development Learning Note 028-uitableview Single-group data display code optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.