Introduction to ios development (III): Introduction to UITableView and iosuitableview

Source: Internet
Author: User

Introduction to ios development (III): Introduction to UITableView and iosuitableview

We have recently started to use uitableview in our project. For more information, I would like to share the usage of uitableview here. If you have any questions, please kindly advise.

To put it bluntly, create a project first. Because Xcode6 removes the empty project option when creating the project, we continue to select single view application. Here we cannot use main storyboard to delete it first, creates a class that inherits from

UINavigationController. The file name is HealthViewcont.

Then in appdelegate

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {

// Override point for customization after application launch.

Return YES;

}

Add the following code:

   self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    [self.window makeKeyAndVisible];    self.window.rootViewController = [[HealthViewcont alloc]init];

The preparation is complete.

-:UITableView Initialization

1. Implement the UITableViewDataSource and UITableViewDelegate proxy protocols in the. h file. If you inherit the UITableView, you do not need to write

Then define two objects

@property(nonatomic)UITableView* tableview; @property(nonatomic)NSMutableArray* dataArryList;

Implemented in the. m file

@synthesize tableview;@synthesize dataArryList;

2. Add the following code in viewdidload:

-(Void) viewDidLoad {[super viewDidLoad]; // initialize a tableview = [[UITableView alloc] initWithFrame: CGRectMake (0, 64, [UIScreen mainScreen]. bounds. size. width, [UIScreen mainScreen]. bounds. size. height-64)]; [self. view addSubview: tableview]; [self. navigationBar setBackgroundColor: [UIColor redColor]; // implement the proxy tableview. delegate = self; tableview. dataSource = self; // initialize data dataArryList = [[NSMutableArray alloc] initWithArray: [NSArray arrayWithObjects: @ "334", @ "445", @ "667 ", @ "779", @ "123", nil]; // Do any additional setup after loading the view, typically from a nib .}

The initialization is complete.

Ii. UITableView data source Initialization

UITableView has three proxy methods that must be implemented

# Pragma mark-Table View // sets the number of sections-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 1;} // sets the number of rows for each section-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [dataArryList count];} // each unit displayed in UITableView is a UITableViewCell object for each TableViewCell operation, its initialization function is initWithStyle: reuseIdentifier: During the Sliding Process of tableView, frequent alloc objects are time-consuming ///, so we introduced the cell reuse mechanism-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * cellIdentifier = @ "cell "; UITableViewCell * cell = [tableView failed: cellIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: Repeated reuseIdentifier: cellIdentifier];} cell. textLabel. text = [dataArryList objectAtIndex: indexPath. row]; return cell ;}

3. insert and delete

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    if (editingStyle == UITableViewCellEditingStyleDelete) {        [self.dataArryList removeObjectAtIndex:indexPath.row];        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];    } else if (editingStyle == UITableViewCellEditingStyleInsert) {        [self.dataArryList insertObject:@"456" atIndex:indexPath.row];        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.    }}

4. Other Common Operations

// Set UITableView row indent-(NSInteger) tableView :( UITableView *) tableView indentationLevelForRowAtIndexPath :( NSIndexPath *) indexPath {NSUInteger row = [indexPath row]; return row ;} // set the height of the cell row interval-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 60 ;} // set the response event of the selected Cell-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {} // set the action performed by the selected row-(NSIndexPath *) tableView :( UITableView *) tableView willSelectRowAtIndexPath :( NSIndexPath *) indexPath {NSUInteger row = [indexPath row]; return indexPath;} // you can specify whether the del button appears in the cell to delete data. (BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {} // set the edit status when deleting-(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {}

Finally, let's take a look at the running effect.

 

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.