UITableView is often used to display data, similar to the ListView in Android, the implementation of UITableView is very simple and inherits from the ListView in Android Uitableviewdatasource ,
Uitableviewdelegate then the corresponding method can be implemented as needed. UITableView has two default built-in styles, plain and grouped,plain indicate that the table view itself does not actually provide a lot of appearance before you actually provide any appearance, and most of the way, The only thing it will do is it will give you these headers and footer. The grouped table view is the grouping style provided by Uikit. Style words if you have special needs, you can also customize the style of grouping.
Page Layout
The page is relatively simple and a simple uitableview:
There is no need to declare in the header file, you need to implement the Protocol:
@interface Viewcontroller:uiviewcontroller <UITableViewDataSource,UITableViewDelegate> @end
Demo Implementation
Declare three data to show the data:
@interface Viewcontroller () { nsarray *channelarr; Nsmutablearray *filmarr; Nsmutablearray *tvarr;} @end
Initialize data:
-(void) viewdidload { [super viewdidload]; Additional setup after loading the view, typically from a nib. Channelarr=[[nsarray alloc] initwithobjects:@ "movie", @ "TV series", Nil]; Filmarr=[[nsmutablearray alloc] initwithobjects:@ "outsmart Tiger Mountain", @ "a step away", @ "rush that year", @ "Beijing love Story", Nil]; Tvarr=[[nsmutablearray Alloc] initwithobjects:@ "Why Sheng Xiao", @ "Blade", @ "clan and flowers Full Floor", @ "Wumei legend", Nil];}
Set number of groups to group:
-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{ NSLog (@ "%lu", (unsigned long) Channelarr.count); return [Channelarr count];}
Set the title of the group:
-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{ return [Channelarr Objectatindex:section];}
Set the number of content under each group:
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{nsinteger count=0 ; Switch (section) {case 0: count=[filmarr count]; break; Case 1: Count=[tvarr count]; break; } return count;}
Set the specific content under each group:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ UITableViewCell *cell =[[uitableviewcell alloc] Initwithstyle:uitableviewcellstyledefault ReuseIdentifier:nil]; Switch (indexpath.section) {case 0: [Cell.textlabel Settext:[filmarr ObjectAtIndex:indexPath.row]]; break; Case 1: [Cell.textlabel Settext:[tvarr ObjectAtIndex:indexPath.row]]; break; } return cell;}
Set the group header and the height of the bottom:
-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) section{ return 40;} -(CGFloat) TableView: (UITableView *) TableView heightforfooterinsection: (nsinteger) section{ return 0;}
To set a click event:
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{ nsstring *content; Switch (indexpath.section) {case 0: content=[nsstring stringwithformat:@ "%@-%@", Channelarr[0],[filmarr ObjectAtIndex:indexPath.row]]; break; Case 1: content=[nsstring stringwithformat:@ "%@-%@", Channelarr[1],[tvarr ObjectAtIndex:indexPath.row]]; break; } Uialertview *alterview=[[uialertview alloc] initwithtitle:@ "Current location:" Message:content delegate:self cancelButtonTitle:@ "OK" otherbuttontitles:nil]; [Alterview show];}
Final effect:
iOS development-uitableview Common methods