IOS learning notes: Implementing a simple table

Source: Internet
Author: User

First, drag the table View to the View window and drag the circle next to the data source and delegate connection to the file's owner icon. In this way, the control class becomes the data source and delegate of the table.

The header file code of the View Controller is as follows:

# Import <uikit/uikit. h> // Let the class followThe uitableviewdelegate and uitableviewdatasource protocols act as the delegate and Data Source of the table view.@ Interface szlviewcontroller: uiviewcontroller<Uitableviewdelegate, uitableviewdatasource>{
// Declare an array for storing the data to be displayedNsarray*Listdata;}@ Property (nonatomic, retain) nsarray* Listdata;@ End

Implementation Code:

#import "SZLViewController.h"@implementation SZLViewController@synthesize listData;- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    NSArray *array = [[NSArray alloc] initWithObjects:@"GuangZhou", @"ShenZhen", @"BeiJing", @"ShangHai", @"TianJing", @"HangZhou", @"NingBo", @"HongKong", @"ZhuHai", @"XiZang", @"XinJiang", @"ChangSha", @"NanNing", @"WuLuMuQi", nil];    self.listData = array;    [array release];}- (void)viewDidUnload{    [super viewDidUnload];    // Release any retained subviews of the main view.    // e.g. self.myOutlet = nil;    self.listData = nil;}-(void)dealloc{    [listData release];    [super release];}

Add the following code:

/*** Check the number of rows in the specified partition. The default number of partitions is 1. This method is used to return the number of rows in the table partitions that constitute the list */-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return [self. listdata count];}/*** this method is called when a table is to draw a row */-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {// static string instance, acting as the key to represent a form element static nsstring * simpletableidentifier = @ "simpletableidentifier"; // simpletableidentifier type reusable unit, uitableviewcell * cell = [tableview identifier: simpletableidentifier]; If (cell = nil) {Cell = [[[uitableviewcell alloc] initwithstyle: Invalid reuseidentifier: simpletableidentifier] autorelease];} // obtain the rows to be displayed in the table view from the indexpath variable nsuinteger ROW = [indexpath row]; // obtain the corresponding string cell from the array. textlabel. TEXT = [listdata objectatindex: Row]; return cell ;}
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.