IOS development-common UITableView Methods

Source: Internet
Author: User

IOS development-common UITableView Methods
UITableView is often used to display data, similar to ListView in Android. Compared with ListView in Android, the implementation of UITableView is very simple. It inherits the UITableViewDataSource, UITableViewDelegate, and then implements the corresponding method as needed. UITableView has two default built-in styles: Plain and Grouped. Plain indicates that the table view itself does not provide many appearances before you actually provide any appearances. In most cases, the only thing it will do is that it will give you these headers and footer. Grouped table view is a grouping style provided by UIKit. You can also customize the group style if you have special requirements. Page Layout page is relatively simple, a simple UITableView: the header file does not need to be declared, You need to implement the following protocol: @ interface ViewController: UIViewController <UITableViewDataSource, UITableViewDelegate> @ endDemo implements three declarations for data display: @ interface ViewController () {NSArray * channelArr; NSMutableArray * filmArr; NSMutableArray * tvArr;} @ end initialization data: -(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. channelArr = [[NSArray alloc] initWithObjects: @ "movie", @ "TV series", nil]; filmArr = [[NSMutableArray alloc] initWithObjects: @ "zhijieweihushan ", @ "one step away", @ "the year in a hurry", @ "Beijing Love Story", nil]; tvArr = [[NSMutableArray alloc] initWithObjects: @ "Why does it mean ", @ "edge", @ "Lu Xiaofeng and Hua manlou", @ "Wu Mei Niang Legend", nil];} set the number of groups:-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {NSLog (@ "% lu", (unsigned long) channelArr. count); return [channelArr count];} set the group title:-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {return [channelArr objectAtIndex: section];} set the number of content in 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 of each group: -(UITableViewCell *) tableView :( UITableView *) tableView metadata :( NSIndexPath *) indexPath {UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle: Invalid reuseIdentifier: nil]; switch. 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 title and bottom height:-(CGFloat) tableView :( UITableView *) tableView heightForHeaderInSection :( NSInteger) section {return 40 ;} -(CGFloat) tableView :( UITableView *) tableView heightForFooterInSection :( NSInteger) section {return 0;} set 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];} source code: // ViewController. m // TableView // http://www.cnblogs.com/xiaofeixiang/// Created by keso on 15/1/24. // Copyright (c) 2015 keso. all rights reserved. // # import "ViewController. h "@ interface ViewController () {NSArray * channelArr; NSMutableArray * filmArr; NSMutableArray * tvArr;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. channelArr = [[NSArray alloc] initWithObjects: @ "movie", @ "TV series", nil]; filmArr = [[NSMutableArray alloc] initWithObjects: @ "zhijieweihushan ", @ "one step away", @ "the year in a hurry", @ "Beijing Love Story", nil]; tvArr = [[NSMutableArray alloc] initWithObjects: @ "Why does it mean ", @ "edge", @ "Lu Xiaofeng and Hua manlou", @ "Wu Mei Niang Legend", nil];} // sets the number of groups-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {NSLog (@ "% lu", (unsigned long) channelArr. count); return [channelArr count];} // set the group title-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {return [channelArr objectAtIndex: section];} //-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) section {// return @ "I am the bottom "; /// set the number of 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 of the Group-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle: Invalid 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;} // The row Height of the group title-(CGFloat) tableView :( UITableView *) tableView heightForHeaderInSection :( NSInteger) section {return 40 ;} -(CGFloat) tableView :( UITableView *) tableView heightForFooterInSection :( NSInteger) section {return 0;} // selected 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];}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ end

Related Article

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.