1. Click on the storyboard file in the project and drag the TableView into the current Uiviewcontroller controller, as shown in
2. Then select TableView, select Prototype Cells inside the attribute, set the parameter to 1, and then set the style to group, a cell space will appear on the TableView, and then drag it into the object you want, I'm going to add it here. Drag in the label
3. Set up an agent for TableView
There are two ways to set up an agent,
1) Connection: Control-connect TableView to current controller Viewcontroller
2) Code setting: Add in VIEWCONTROLLER.M file which need agent
Self.tableView.delegate = Self;self.tableview.datasource = self;
After Setup, drag the TableView to the ViewController.h file to set the association
4. Custom cell
Create a new simpletableviewcell.m file, then select the cell inside the previous storyboard, add the Class setting association, and then drag the label inside the cell into the SimpleTableView.h file.
5. In the Viewcontroller need to implement the TableView first off
1) Add the following code to the VIEWCONTROLLER.M file @interface
@interface Viewcontroller () <UITableViewDelegate,UITableViewDataSource>
2) Common methods
/******************uitableview********************************/-(Nsinteger) Numberofsectionsintableview: ( UITableView *) tableview{return 1;} How many lines #pragma mark a group-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{NSLog (@ "Altogether%ld lines", Tabledata.count); return tabledata.count;} #pragma the corresponding data in Mark Indexpath-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: ( Nsindexpath *) indexpath{NSLog (@ "TableView Loading data ... "); Person *person = Tabledata[indexpath.row]; static NSString *simpletableidentifier = @ "Simpletableviewcell"; Simpletableviewcell *cell = (Simpletableviewcell *) [_tableview dequeuereusablecellwithidentifier: Simpletableidentifier]; Cell.name.text = Person.personname; Cell.describe.text = Person.persondescribe; Cell.icon.image = [UIImage ImageNamed:person.personIcon]; return cell;} #pragma mark returns the cell height of the Indexpath line-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (nsindexpAth *) indexpath{return 72;} #pragma mark Click Event-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{NSLog (@ "Click here");} -(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath {//Return NO if you don't Want the specified item to be editable. return YES;} #pragma mark left swipe to select Delete-(void) TableView: (UITableView *) TableView Commiteditingstyle: (Uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath {if (Editingstyle = = Uitableviewcelleditingstyledelete) { [Tabledata RemoveObjectAtIndex:indexPath.row]; [TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; } else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {//Create a new instance of the appropriate class, Insert it into the array, and add a new row to the table view. }}/******************uitableview*******end**********************/
where person isdefines a class that contains three attributes, such as Personname,persondescribe,personicon, and then defines a class initialization method (similar to a constructor)
+ (person*) Initpersonewithname: (nsstring*) name Describe: (nsstring*) Describe icon: (nsstring*) icon;
Another class that needs to be explained is the Simpletableviewcell class, which is inherited from UITableViewCell and can be used to customize the data in the TableView.
Display, the main code is as follows:. h file
simpletableviewcell.h// pishumtableview//// Created by Pishum on 15/7/29.// Copyright (c) 2015 Pishum. All rights reserved.//#import <UIKit/UIKit.h> @interface simpletableviewcell:uitableviewcell@property (Strong, nonatomic) Iboutlet UILabel *name; @property (Strong, nonatomic) Iboutlet UILabel *describe; @property (Strong, Nonatomic) Iboutlet Uiimageview *icon; @end
. m file
simpletableviewcell.m// pishumtableview//// Created by Pishum on 15/7/29.// Copyright (c) 2015 Pishum. All rights reserved.//#import "SimpleTableViewCell.h" @implementation simpletableviewcell-(void) awakefromnib { / /initialization code}-(void) setselected: (BOOL) selected animated: (bool) animated { [Super setselected:selected Animated:animated]; Configure The view for the selected state} @end
To be reminded, when using storyboard, in the above 2 steps, set the TableView cell, you need to associate the
Simpletableviewcell class, but also to set it in storyboard identifier, where I set the identifier is "Simpletableviewcell" and
The class name is the same, so you can avoid forgetting.
Just add data to Tabledata, here is a simple add example, of course, there are a lot of methods here, not much to say
-(nsmutablearray*) gettabledata{ nsmutablearray * array = [[Nsmutablearray alloc]init]; Person *person1 =[person initpersonewithname:@ "Liu Bei" describe:@ "double strand Sword" icon:@ "Switch.png"]; Person *person2 =[person initpersonewithname:@ "Guan Yu" describe:@ "Tsing Lung Yan Yue Dao" icon:@ "Switch.png"]; Person *person3 =[person initpersonewithname:@ "Zhang Fei" describe:@ "Zhang Eight Snake spear" icon:@ "Switch.png"]; [Array addobject:person1]; [Array addobject:person2]; [Array Addobject:person3]; return array;}
Source
Https://github.com/pishum/PishumTableView
This article original, reproduced please indicate the source
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Xcode6 iOS Development UITableView Storyboard-based use