IOS Custom UITableView

Source: Internet
Author: User

According to different needs, need to use the structure of tableview, but each cell inside, but also need their own style, so learned how to add their own cell definition of the tableview inside



First you create a class yourself, inherit UITableViewCell, and then create a new empty Xib file, and set the Class property to the corresponding name







Code section:

<pre name= "code" class= "OBJC" > #import "SettingViewController.h" #import "SettingViewCell.h" @interface Settingviewcontroller () @end @implementation settingviewcontroller@synthesize listarray =_listArray;-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [Super Initwithnibname:    Nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;}    -(void) viewdidload{[Super Viewdidload];    Do any additional setup after loading the view from its nib.    Nsarray *array = [Nsarray arraywithobjects: @ "Room settings", @ "Lighting settings", @ "Curtain Settings", @ "Scene settings", @ "Air conditioning settings", @ "Security settings", @ "Network Settings", nil]; _settingview.datasource =self; Set the data source method _settingview.delegate =self;//Set the proxy method Self.listarray =array; Note that due to memory management problems, if Self.listarray is not assigned, _listarray will be emptied and the program will crash}-(ibaction) backclicked: (ID) Sender {[ Self.navigationcontroller Poptorootviewcontrolleranimated:yes];} Data source Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{return [_listarray count];} Delegate Method-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (nsindexpath *) indexpath{switch (        Indexpath.row) {case 0:break;    Default:break; }}//Row display.  Implementers should *always* try to reuse cells by setting each cell's reuseidentifier and querying for available reusable Cells with dequeuereusablecellwithidentifier://Cell gets various attributes set automatically based on table (separators ) and data source (accessory views, editing controls)-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowat    Indexpath: (Nsindexpath *) indexpath{static NSString * Cellidentifier [email protected] "MyCell";    Settingviewcell *cell = (Settingviewcell *) [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) {Nsarray *array =[[nsbundle mainbundle] loadnibnamed:@ "Settingviewcell" Owner:self Options:nil]        ; cell = [Array objecTATINDEX:0];    [Cell Setselectionstyle:uitableviewcellselectionstylegray];     } nsstring *text = _listarray[indexpath.row];    [[Cell Settingname] settext:text]; return cell;}    -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.}    -(void) Dealloc {[_back release];    [_settingview release]; [Super Dealloc];} @end




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.