"iOS dev-59" LOL Case: Single-group Tabview, Alertview style, monitor implementation, and refresh with Reloaddata data

Source: Internet
Author: User

Case Effect:


(1) First drag and drop a tableview in the storyboard, and then use the following code.

--tableview inherits from ScrollView, so nature has scrolling characteristics

--The most important is the data transfer model, and the assignment to the cell

--and the cell's assignment, to optimize performance, we first look in the TableView cache for the cell with or without the cache, if there is, take it directly, if not created again, this improves performance.

-This buffer pool is tableview, and when the cell is not in the line of sight when scrolling, the cell is placed in the cache pool.

#import "ViewController.h" #import "WSHeros.h" @interface Viewcontroller () <UITableViewDataSource> @property ( Weak, nonatomic) Iboutlet UITableView *tableview; @property (strong,nonatomic) Nsarray *herosarray; @end @implementation    viewcontroller-(void) Viewdidload {//defines who the data source is self.tableview.datasource=self;    Height self.tableview.rowheight=60 per row;    Line divider color and style self.tableview.separatorcolor=[uicolor colorwithred:0 green:0 blue:1 alpha:1];    Self.tableview.separatorstyle=uitableviewcellseparatorstylenone;    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} Hide Status bar-(BOOL) prefersstatusbarhidden{return YES;} Set number of lines-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return self.herosarray.count;//array number of rows}//set cell contents per row-(UITableViewCell *) TableView: (UITableView *) TableView    Cellforrowatindexpath: (Nsindexpath *) indexpath{static nsstring *[email protected] "hero"; 1, First Judge TablevieW There is no cache cell we need, identify UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:id] with ID class;  2, if not, create directly, remember to give ID identification number if (Cell==nil) {Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle    Reuseidentifier:id];    }//either directly or directly, as of here, there is a cell.    Using Indexpath.row to obtain the corresponding model of line number Wsheros *hero=self.herosarray[indexpath.row];    The cell is then assigned a value of cell.textlabel.text=hero.name;    Cell.imageview.image=[uiimage ImageNamed:hero.icon];    Cell.detailtextlabel.text=hero.intro;    Cell.accessorytype=uitableviewcellaccessorydisclosureindicator;    The cell also has the background and the option of selecting the background cell.backgroundcolor=[uicolor Graycolor];    UIView *bgview=[[uiview Alloc]init];    Bgview.backgroundcolor=[uicolor Redcolor];    Cell.selectedbackgroundview=bgview; return cell;} Dictionary to model-(Nsarray *) herosarray{if (_herosarray==nil) {nsstring *path=[[nsbundle mainbundle]pathforresource:@ "he        Ros.plist "Oftype:nil";        Nsarray *arr=[nsarray Arraywithcontentsoffile:path];Nsmutablearray * Herosmuarr=[[nsmutablearray Alloc]init];            For (Nsdictionary * dict in arr) {Wsheros *heros=[[wsheros alloc]initwithdict:dict];        [Herosmuarr Addobject:heros];    } _herosarray=herosmuarr; } return _herosarray;} @end

(2) Increase the effect of click Pop Alert, and can change the name

--you need to introduce a protocol because you use the listen TableView click

--because the listener user clicked on which button in alert, a protocol is required

@interface Viewcontroller () <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

and need to set up the agent (alert agent, when created directly designated as self can)

-(void) viewdidload {   ...    self.tableview.delegate=self;   ......}


--When the listener TableView is clicked, it needs to pop up a box with TextField, which can be set with the Alert.alertviewstyle property, and assign the name in the model to the text box.

--In addition, it is necessary to take the first model, this number recorded, in the next method to listen to the click on the alert which button which needs to be used, just recorded in the alert tag.

--Listen for the "OK" button when clicked, and if so, get text box text first, Then use the Alert.tag to find the corresponding data model, with the text to replace the original model data, and finally refresh the TableView, only modify the data model, to completely change the TableView display results, otherwise only modify TableView, and so reload when the original data will be displayed, Because the data model is not modified.

The idea is to control the view with the data model, that is, to modify the data model, and the rendering of the view changes naturally.

-The knowledge points used here are relatively large, involving the Reloaddata method of TableView.

--related to Alertview style setting method.

-It also involves the use of tableview and Alertview agents for monitoring.

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{Wsheros *hero=    Self.herosarray[indexpath.row]; Uialertview *alert=[[uialertview alloc]initwithtitle:@ "hint" Message:nil delegate:self cancelButtonTitle:@ "Cancel"    otherbuttontitles:@ "good", nil];    Alert.alertviewstyle=uialertviewstyleplaintextinput;    [Alert Textfieldatindex:0].text=hero.name;    Alert.tag=indexpath.row; [Alert show];}    -(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) buttonindex{if (buttonindex==0) return;    Wsheros *hero=self.herosarray[alertview.tag];    Hero.name=[alertview Textfieldatindex:0].text; You need to pass a indexpath array here, because you might refresh more than one line, so you need to know a few lines, and then pass many of the constituent arrays in Nsindexpath *path=[nsindexpath Indexpathforrow:    Alertview.tag insection:0];    [Self.tableview Reloadrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationbottom]; The following is a refresh of all data//[Self.tableview reloaddata];}

Final effect:


"iOS dev-59" LOL Case: Single-group Tabview, Alertview style, monitor implementation, and refresh with Reloaddata data

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.