Basic usage of uitableview

Source: Internet
Author: User

1. When do I need to use uitableview?

The official documentation is as follows:

 

 

  • To let users navigate through hierarchically structured data

  • To present an indexed list of items

  • To display detail information and controls in visually distinct groupings

  • To present a selectable list of options

A simple understanding is that the actual data has a certain level of structure.

2. The presentation of uitableview data is configured by Delegate and datasource (this is very important !!!)

3. uitableview type plain (no interval) and grouped (interval between segments)

Code

1. Create a tableview

@ Interface viewcontroller () <uitableviewdelegate, uitableviewdatasource> // the two protocols must comply

1 // create a tableview object 2 self. tableview = [[uitableview alloc] initwithframe: Self. view. bounds style: uitableviewstyleplain]; 3 // set delegate and datasouce4 _ tableview. delegate = self; 5 _ tableview. datasource = self; 6 // Add to interface 7 [self. view addsubview: _ tableview];

2. Configure delegate and datasource (required)

1 // configure the number of rows in each section (segment) Cell 2 // by default, there is only one section 3-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) section {4 return 5; 5} 6 // What is displayed in each row 7-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {8 // set the ID number for each cell (used for reuse) 9 static nsstring * cellid = @ "cellid "; 10 11 // obtain a cell12 uitableviewcell * cell = [t from a queue in tableview Ableview dequeuereusablecellwithidentifier: cellid]; 13 14 // determine whether the cell is not created by itself in the queue. If 15 if (cell = nil) {16 // No, create a 17 cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellid]; 18 19} 20 21 // use cell22 cell. textlabel. TEXT = @ "Haha !!! "; 23 return cell; 24}

Running result

 

Optional

1 // configure multiple sections 2-(nsinteger) numberofsectionsintableview :( uitableview *) tableview {3 return 6; // segment 6 4} 5 6 // set height 7-(cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath {8 return 60; 9} 10 11 // a cell is clicked 12-(void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {13 // deselect 14 [tableview deselectrowatindexpath: indexpath animated: Yes]; 15}

Effect

 

 

 

Basic usage of uitableview

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.