iOS development-uitableview Common methods

Source: Internet
Author: User

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

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.