IOS: Implementing table Fill and select operations

Source: Internet
Author: User
Tags uikit

Function: Create a list, populate the table with an array, and support select list rows

////main.m//Hello////Created by Lishujun on 14-8-28.//Copyright (c) 2014 Lishujun. All rights reserved.//#import<UIKit/UIKit.h>//View Controller Objects@interfaceHelloworldviewcontroller:uiviewcontroller<uitableviewdatasource, uitableviewdelegate>{Nsarray*Array; UITableView*TableView; //without a @property declaration, you can't use self for reference? I have to figure out what's the difference .}@end@implementationHelloworldviewcontroller-(Helloworldviewcontroller *) init{Array= [[Nsarray alloc]initwithobjects:@"oops",@"Hello",@" World", nil]; returnSelf ;}-(void) loadview{//Create a View objectUIView *contentview =[[UIView Alloc]initwithframe:[[uiscreen Mainscreen] applicationframe]; Contentview.backgroundcolor=[Uicolor Lightgraycolor]; Self.view=Contentview; //Create TableViewTableView =[[UITableView alloc]initwithframe:[[uiscreen Mainscreen] applicationframe];    [TableView setdatasource:self];        [TableView setdelegate:self]; //TableView Adding to the content view[Contentview Addsubview:tableview];}//--------The method that the DataSource interface must implement to populate the table------------(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;//set Number of list partitions}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return[Array Count];//set list length}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//Generate list cells (reuse existing units as much as possible)    StaticNSString *simpletableidentifer =@"Simpletableidentifer"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:simpletableidentifer]; if(Cell = =Nil) {Cell=[[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:simpletableidentifer]; } Cell.textLabel.text=Array[indexpath.row]; returncell;}//----------method for implementing the delegate interface for Operation----------------(Nsindexpath *) TableView: (UITableView *) TableView Willselectrowatindexpath: (Nsindexpath *) indexpath{//Row to select the previous event, the following code implementation is the choice to implement canceling the first row, that is, not allowed to select    if(Indexpath.row = =0)    {        returnNil; }    Else    {        returnIndexpath; }}-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{//Row Check EventNSString *rowvalue =Array[indexpath.row]; [TableView Deselectrowatindexpath:indexpath Animated:yes]; //Uncheck StateNSLog (@"%@", Rowvalue); }@end//Delegate Object@interfaceHelloworldappdelegate:nsobject <UIApplicationDelegate>{iboutlet UIWindow*window;} @property (nonatomic, retain) UIWindow*window;@end@implementationhelloworldappdelegate@synthesizewindow;-(void) Applicationdidfinishlaunching: (UIApplication *) application{Self.window=[[UIWindow alloc] Initwithframe:[[uiscreen Mainscreen]bounds]]; Helloworldviewcontroller*viewcontroller =[[Helloworldviewcontroller alloc]init]; Self.window.rootViewController=Viewcontroller; [Self.window makekeyandvisible];}@end//Program EntryintMainintargcChar*argv[]) {@autoreleasepool {returnUiapplicationmain (argc, argv, Nil,@"helloworldappdelegate"); }}

IOS: Implementing table Fill and select operations

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.