Case Effect:
(1) First drag and drop a tableview in the storyboard. Then use the code below.
--tableview inherits from ScrollView. So naturally there is the characteristic of rolling
--The most basic 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 a cell with or without a cache, fake it, and take it out directly, assuming it's 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 cut lines 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 infer TablevieW has a cell that we do not need to cache. Identify UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:id] with ID class; 2, if not, it is created directly. Remember to give ID identification number if (Cell==nil) {Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseIdentifie R:id]; }//either direct fetch. or create it directly. As of here, there is a cell. First use 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) Add click to Pop alert effect, and can change the name
--you need to introduce a protocol because you use the monitor TableView click
-A protocol is required because the listener user clicked on which button in the alert
@interface Viewcontroller () <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
and need to set the agent (alert agent, when created directly designated as self can)
-(void) viewdidload { ... self.tableview.delegate=self; ......}
--When the monitor tableview is clicked, a box with a textfield is required to pop up. Can be set with the Alert.alertviewstyle property, and the name inside the model is assigned to the text box display.
In addition, it is necessary to record the number of models. In the next method of listening to the click Alert which one of the button needs to be used, just recorded in the alert tag.
--The "OK" button when the listener is clicked. Suppose it is, get the text box text first, and then use Alert.tag to find the corresponding data model. Replace the original model data with this obtained text, and finally refresh the TableView, just change the data model. Ability to completely change the TableView display results, or just change the tableview, and so on again when loading the original data will be displayed. Because the data model has not changed.
The idea is: using the data model to control the view, that is, changing the data model, the view of the rendering naturally followed by changes.
-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 it is possible to refresh more than one line, so you need to know a few sets of rows, and then pass the very many constituent arrays into Nsindexpath *path=[nsindexpath Indexpathforrow : Alertview.tag insection:0]; [Self.tableview Reloadrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationbottom]; Here is the refresh of all data// [Self.tableview reloaddata];}
Finally effect:
"iOS dev-59" LOL Case: Single-group Tabview, Alertview style, monitor implementation, and refresh with Reloaddata data