1 Preface
Uitableview is divided into different rolling views, each part is divided into different rows, you can also create custom tableview rows. Uitableview implements vertical scrolling of uiscrollview. You can set the height and number of rows of each row and the content of each row.
2. code example
Zyviewcontroller. h:
# Import <uikit/uikit. h> @ interface zyviewcontroller: uiviewcontroller <uitableviewdelegate, uitableviewdatasource> // Add a proxy @ property (nonatomic, strong) uitableview * mytableview; @ end
Zyviewcontroller. M:
@ Synthesize mytableview;-(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view, typically from a nib. self. view. backgroundcolor = [uicolor whitecolor]; mytableview = [[uitableview alloc] initwithframe: Self. view. bounds style: uitableviewstyleplain]; // you can specify uitableviewstylegrouped as the group mode and uitableviewstyleplain as the normal style. mytableview. delegate = self; // sets the proxy For its own mytableview. datasource = self; // set the data source to self. mytableview. autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight; // ensure that the tablview can adjust the size correctly [self. view addsubview: mytableview];} // set the height of each row-(cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath {cgfloat result = 20f F; if ([tableview isequal: Self. mytableview]) {// result = 40366f; resu Lt = 80366f;} return result;} // allows the data source to indicate the number of sections of the table that must be loaded to table view. -(Nsinteger) numberofsectionsintableview :( uitableview *) tableview {nsinteger result = 0; If ([tableview isequal: mytableview]) {result = 3; // a total of three sections} return result;} // you can specify the number of rows in each section-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {nsinteger result = 0; if ([tableview isequal: mytableview]) {Switch (section) {// if it is the first section, three rows of data are displayed. Case 0: Result = 3; break; Case 1: Result = 5; break; Case 2: Result = 8; break;} return result;} // data in each row-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {uitableviewcell * result = nil; If ([tableview isequal: mytableview]) {static nsstring * tableviewcellidentifier = @ "mycells "; // set the cell id result = [tableview dequeuereusablecellwithidentifier: tableviewcellidentifier]; // Return a reusable table view cell object if (result = nil) {result = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: tableviewcellidentifier] through the identifier; // initialize a table cell style and reuse identifier, and return it to the caller.} // Indexpath. section indicates the index indexpath of the Section. row indicates the index result of the number of rows. textlabel. TEXT = [nsstring stringwithformat: @ "section % lD, cell % lD", (long) indexpath. section, (long) indexpath. row];} return result ;}
// The event triggered when a row is clicked-(void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {If ([tableview isequal: mytableview]) {nslog (@ "% @", [nsstring stringwithformat: @ "cell % LD in section % LD is selected", (long) indexpath. row, (long) indexpath. section]) ;}}
Running result:
Click the result displayed on the console when a row is clicked:
06:46:28. 021 uitableviewtest1 [425: c07] Cell 4 in section 1 is selected
06:46:28. 869 uitableviewtest1 [425: c07] Cell 0 in section 2 is selected
06:46:31. 012 uitableviewtest1 [425: c07] cell 1 in section 2 is selected
3 conclusion
The above is a brief introduction to uitableview, hoping to help you.
Demo: http://download.csdn.net/detail/u010013695/5309989