1.2. 3.UITableView Protocols:how Do we connect to all this stuff in our code? Via the UITableView ' s DataSource and delegate. The delegate is used to control
HowThe table is displayed. The DataSource provides the data
Whatis displayed inside the cells. Uitableviewcontroller:automatically sets itself as its UITableView ' s delegate & Datasource.also have a property pointin G to its UITableView:
@property (Nonatomic,strong) UITableView *tableview;(This property is actually = = Self.view in uitableviewcontroller!) 4.uitableviewdatasource:we has to implement these 3 to bes a "dynamic" (arbitrary number of rows) Table ... How is many sections in the table? How many rows in each section? Give me a uitableviewcell to draw each cell at a given row in a given section. 5.How do we control what are drawn in each cell in a dynamic table? Each row was drawn by its own instance of UITableViewCell (a UIView subclass). Here's the Uitableviewdatasource method to get this cell for a given row in a section ...
//Nsindexpath is just a object with the important properties for use with Uitableview:row and sections.-(UITableViewCell *) TableView: (UITableView *) sender Cellforrowatindexpath: (Nsindexpath *) indexpath{//get a cell to use (instance of UITableViewCell)UITableViewCell *cell; Cell=[Self.tableview dequeuereusablecellwithidentifier:@ "Flickr Photo Cell" Forindexpath:indexpath]; //set @properties on the cell to prepare to display//There is obviously other things you can does in the cell besides setting its text (Detail text,image,checkmark,etc.). //See how we is using indexpath.section and Indexpath.row to get the Model information to set up the This cell.Cell.textLabel.text =[self getMyTitleForRow:indexPath.row inSection:indexPath.section]; returncell;}
The cells in the table is actually reused. When the one goes off-screen,it gets put into a "reuse pool." The next time a cell is Needed,one are grabbed from the reuse pool if available. If none is Available,one would be put to the reuse pool if there ' s a prototype in the storyboard. Otherwise this dequeue method would return nil. 6.How does a dynamic table know how many rows there is? And how many Sections,too, of course? Via These-uitableviewdatasource methods ...
-(Nsinteger) Numberofsectionsintableview: (UITableView *) sender; -(Nsinteger) TableView: (UITableView *) sender Numberofrowsinsection: (nsinteger) Section;
Number of sections is 1 by default. No default for Tableview:numberofrowsinsection:,this are a required method in this Protocol (as is Tableview:cellforrowatind Expath:). Do not implement these DataSource methods for a static table. Uitableviewcontroller the You. 7.uitableviewdelegate:the delegate controls how the UITableView is displayed. What does it displays (that's the DataSource ' s job). The delegate also lets you observe, the table view is doing:the classic "will/did" sorts of things. An important one was "user did select a row." Usually we don t need this because we simply segue when a row is touched. But there was some occasions where it'll be the Useful... 8.uitableviewdelegate method sent when row is Selected:this I s sort of "TableView target/action" (only needed if you ' re not segueing,of course). On the Ipad,where the table might is on screen with what it updates,you might need this.
-(void) TableView: (UITableView *) sender Didselectrowatindexpath: (Nsindexpath *) path{ // go do something based on information about my model //corresponding to Indexpath.row in IND Expath.section}
Remember the little curled I? Clicking on this would not segue. Instead It'll invoke this method in the Uitableviewdelegate protocol ...
-(void ) TableView: (UITableView *) sender Accessorybuttontappedforrowwithindexpath: (Nsindexpath *) indexpath{ // do something related to the row at Indexpath, // but not the primary action associated with touching the row }
9.UITableView segue:the Sender of PrepareForSegue:sender:is The Uitableviewcell.use the important method Indexpathforcell:to find out the indexpath of the row that ' s segueing.-(void) Prepareforsegue: (Uistoryboardsegue *) Segue S Ender: (ID) sender{Nsindexpath *indexpath = [Self.tableview Indexpathforcell:sender]; Prepare Segue.destinationcontroller to display based on information//about my Model corresponding to Indexpath.row In Indexpath.section} 10.UITableViewController have a "activity indicator" built in. You get it via the Uitableviewcontroller @property (strong) Uirefreshcontrol *refreshcontrol; Start it with...-(void) beginrefreshing; Stop it with...-(void) endrefreshing; 11.What If your Model changes?
-(void) reloaddata;Causes the table view to call Numberofsectionsintableview:and Numberofrowsinsection:all over again and then Cellforrowat Indexpath:on each visible cell. Relatively heavyweight,but if your entire data structure Changes,that ' s what you need. If only part of your Model Changes,there is lighter-weight reloads,for examples ...
-(void) Reloadrowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) AnimationStyle ;
Stanford University Open Class: IOS 7 App Development Lecture11