iOS Development learning note 027-uitableview using Model objects

Source: Internet
Author: User

1. Model objects

Continue to optimize the previous program

The last time I used a dictionary, but it was a lot of harm. This encapsulates the data in a class.

This is the model of MVC, and the model is the display structure of the data.

Create a new class, add several properties, and a class method to quickly return an object

1 #import<Foundation/Foundation.h>2 3 @interfaceProvince:nsobject4 //UI controls with weak5 //nsstring with Copy6 7 8@property (nonatomic,copy) NSString *header;9@property (nonatomic,copy) NSString *footer;Ten@property (nonatomic,strong) Nsarray *cities; One // A+ (ID) Provincewithheader: (NSString *) header andfooter: (NSString *) footer andcities: (Nsarray *) cities; - @end

1 #import "Province.h"2 3 @implementationProvince4 5 6+ (ID) Provincewithheader: (NSString *) header andfooter: (NSString *) footer andcities: (Nsarray *) Cities7 {8Province *pp =[[Province alloc] init];9Pp.footer =footer;TenPp.header =header; OnePp.cities =cities; A     returnpp; - } - @end

Using class methods when initializing objects

1Province *gd = [Province Provincewithheader:@"Guangdong"Andfooter:@"Guangdong anger Ah Ah"andcities:@[@"Guangzhou",@"Shenzhen"]];2Province *hn = [Province Provincewithheader:@"Hunan"Andfooter:@"Hunan hahaha Ah AH"andcities:@[@"Changsha",@"Yueyang"]];

Modify the method Numberofrowsinsection

1 // 2 Set how many rows per group row 2 -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger)Section3  {4     //  Remove data from Object 5     return  [_ Allprovince[section] Cities].count; 6 }

Modify the method Cellforrowatindexpath

1 //3 Returns the content displayed for each row2 //Indexpath identifies a unique row, the first few lines of the group3-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath4 {5UITableViewCell *tablecell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:nil];6     //set the text displayed by the cell7NSString *text =[_allprovince[indexpath.section] cities][indexpath.row];8TableCell.textLabel.text =text;9     returnTableCell;Ten}

Modify the method Titleforheaderinsection

1 #pragma Mark section header 2 -(NSString *) TableView: (UITableView *) TableView Titleforheaderinsection: (nsinteger)section3{4     return  [_ Allprovince[section] [header]; 5 }

After modifying the titleforfooterinsection

1 #pragma The tail of the Mark section group 2 -(NSString *) TableView: (UITableView *) TableView Titleforfooterinsection: (nsinteger)section3{4     return  [_ Allprovince[section] footer]; 5 }

The effect is the same, but the code is much more extensible.

Displays the index to the right of the table

Need to use a method

-(Nsarray *)sectionindextitlesfortableview:(UITableView *) TableView

The returned array is the indexed array to be realistic, and clicking on the index text jumps to the corresponding group

1 #pragmaMark returns the index bar that appears to the right of the table2-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView3 {4Nsmutablearray *title =[Nsmutablearray array];5      for(Province *pinch_allprovince)6     {7[Title Addobject:p.header];//gets the caption displayed in the index8     }9     returntitle;Ten}

2, single group of data display

All of the above is the display of multiple sets of data, the following is a single set of data display.

The main is to specify the style parameter as plain when creating the view

Set up groups and rows

1 //set the line, since it is a single group, there is only one row2-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView3 {4     return 1;5 }6 //Set Line7-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section8 {9     return 9;Ten } One //Set Line Contents A-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath - { -UITableViewCell *cell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:nil]; theCell.textLabel.text = [NSString stringWithFormat:@"Row%d data", Indexpath.row];//Middle Text -Cell.imageView.image = [UIImage imagenamed:[nsstring stringWithFormat:@"00%d.png", Indexpath.row +1]];//left Image -Cell.detailTextLabel.text = [NSString stringWithFormat:@"description of row%d data", Indexpath.row];//Description text, description of the textlable -     returncell; +}

several display modes of UITableViewCell:

Uitableviewcellstyledefault: Do not display sub-headings

Uitableviewcellstylevalue1:detial label appears on the right

UITableViewCellStyleValue2: Do not display pictures

Uitableviewcellstylesubtitle: Show sub-headings

Set the button or icon that appears on the far right

Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; Right-most indication information

Uitableviewcellaccessorycheckmark display a checkmark at the far right

Uitableviewcellaccessorydetailbutton Show an I button on the far right

Uitableviewcellaccessorydetaildisclosurebutton Display an I button and an angle bracket >

Uitableviewcellaccessorydisclosureindicator Display an angle bracket >

The end result is this

The code is as follows

1 //2 //SLQVIEWCONTROLLER.M3 //uitableview-Single-group data display4 //5 //Created by Christian on 15/5/16.6 //Copyright (c) 2015 SLQ. All rights reserved.7 //8 9 #import "SLQViewController.h"Ten  One @interfaceSlqviewcontroller () <uitableviewdatasource, uitableviewdelegate> A  - @end -  the @implementationSlqviewcontroller -  -- (void) Viewdidload - { + [Super Viewdidload]; -     //additional setup after loading the view, typically from a nib. +      A } at  -  - //set the line, since it is a single group, there is only one row --(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView - { -     return 1; in } - //Set Line to-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section + { -     return 9; the } * //Set Line Contents $-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexpathPanax Notoginseng { -UITableViewCell *cell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:nil]; theCell.textLabel.text = [NSString stringWithFormat:@"Row%ld Data", Indexpath.row];//Middle Text +Cell.imageView.image = [UIImage imagenamed:[nsstring stringWithFormat:@"00%ld.png", Indexpath.row +1]];//left Image ACell.detailTextLabel.text = [NSString stringWithFormat:@"description of the row of data in page%ld", Indexpath.row];//Description Information theCell.accessorytype = Uitableviewcellaccessorydisclosureindicator;//right-most indication information +     returncell; - } $  $  - @end

iOS Development learning note 027-uitableview using Model objects

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.