"iOS Development-58" TableView: The use of 5 important methods and the difference between 2 different styles

Source: Internet
Author: User

Create a TableView, and drag and drop the storyboard inside.

(1) First create a data model class Wscargroup, in the WSCarGroup.h file:

#import <Foundation/Foundation.h> @interface wscargroup:nsobject@property (nonatomic,copy) NSString * title;@ Property (nonatomic,copy) NSString * DESC; @property (nonatomic,strong) Nsarray *cars; @end

(2) in VIEWCONTROLLER.M:

--First turn the data into a model, there is no plist, so direct manual input.

Declare an array variable to load the model first

@property (Nonatomic,strong) Nsarray *cargroups;

-(Nsarray *) cargroups{    if (_cargroups==nil) {        wscargroup *cg1=[[wscargroup alloc]init];        [Email protected] "German car";        [Email protected] "The Most excellent quality";        [Email protected] [@ "BMW", @ "Audi", @ "Mercedes"];                Wscargroup *cg2=[[wscargroup Alloc]init];        [Email protected] "Japanese car";        [Email protected] "very light and very fuel-saving";        [Email protected] [@ "Mitsubishi", @ "Nissan", @ "Honda", @ "Mitsubishi", @ "Nissan", @ "Honda", @ "Mitsubishi", @ "Nissan", @ "Honda", @ "Mitsubishi", @ "Nissan", @ "Honda"];                [Email protected] [CG1,CG2];    }    return _cargroups;}


--Set the TableView data source, similar to the Protocol, to act as a data source to comply with the "protocol." Here we take viewcontroller as a data source. So observe first:

@interface Viewcontroller () <UITableViewDataSource>


--then set up as a data source

-(void) viewdidload {    self.tableview.datasource=self;    [Super Viewdidload];}

Some of the most important methods of--tableview

111, set how many groups (if this method is omitted, the default is 1 groups)

222. Set how many rows each group has

333. Set what the cell in each row fills (most important)

444. Set header text for each group

555. Set Meixu Trailing description text

-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return self.carGroups.count;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{Wscargroup *cg=    Self.cargroups[section]; return cg.cars.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//    Because you want to return UITableViewCell, you first create one, then assign the value, and finally return.    UITableViewCell *cell=[[uitableviewcell Alloc]initwithstyle:uitableviewcellstyledefault ReuseIdentifier:nil];    Wscargroup *cg=self.cargroups[indexpath.section];    Cell.textlabel.text=cg.cars[indexpath.row]; return cell;} -(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{Wscargroup *cg=    Self.cargroups[section]; return cg.title;} -(NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (nsinteger) section{Wscargroup *cg=    Self.cargroups[section]; return CG.DESC;}

--of course, to show convenience, hide the top status bar

-(BOOL) prefersstatusbarhidden{    return YES;

TableView has two forms of presentation: plain and grouped.

--plain is the interval between each group is very small, the grouping is not obvious. Grouped each group is significantly separated by large groupings. As follows:


--plain has a head hover effect, similar to the group name of QQ group Friends hover at the top, until the next group is replaced.


"iOS Development-58" TableView: The use of 5 important methods and the difference between 2 different styles

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.