Examples of IOS application development using UITableView to create a custom table _ios

Source: Internet
Author: User
Tags set background

A table view with indexed catalogs

1. Effect drawing

2. Data source

I would like to get the name of the address book, but in order to use the simulator debugging convenient, wrote dead data, so also only wrote part of the letter, in short, so little meaning on the

Copy Code code as follows:

@interface Viewcontroller () <UITableViewDataSource,UITableViewDelegate>
{
Nsarray *sectiontitles; Title of each section
Nsarray *contentsarray; Contents of each line
}

/** @brief Prepare the data source to be invoked in the Viewdidload method * *
-(void) Readysource
{

Sectiontitles = [[Nsarray alloc] Initwithobjects:
@ "A", @ "C", @ "F", @ "G", @ "H", @ "M", @ "S", @ "T", @ "X", @ "Z", nil];
Contentsarray = [[Nsarray alloc] Initwithobjects:
@[@ "Ah Wei", @ "Auntie", @ "Ah san"],
@[@ "Cai Core", @ "Cheng Long", @ "Chen Xin", @ "Chen Dan Works", @ "famous"],
@[@ "Fang Zi", @ "Jaycee name", @ "square Datong", @ "Fang Fang", @ "Fanwei"],
@[@ "Guo Jing", @ "Guo Meimei", @ "too son", @ "roller coaster"],
@[@ "Xiangu", @ "and Shen", @ "Mr. Uwechue", @ "Good man"],
@[@ "mother", @ "Chairman Mao"],
@[@ "Sun Yat-sen", @ "Shen Bing", @ "Aunt"],
@[@ "Tao Tao", @ "Taobao", @ "set Eva"],
@[@ "Small Two", @ "Summer Crape myrtle", @ "Xu Wei", @ "Xu Qing"],
@[@ "Zhou Enlai", @ "Jay Chou", @ "Cecilia Cheung", @ "Zhang Daxian"],nil];
}


3. Display Index
Copy Code code as follows:

Header for each partition
-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section
{
return [Sectiontitles objectatindex:section];
}
Index Directory
-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView
{
return sectiontitles;
}
④ Click on the index and jump to the clicked partition

Click Catalogue
-(Nsinteger) TableView: (UITableView *) TableView sectionforsectionindextitle: (NSString *) title Atindex: (NSInteger) Index
{
Gets the Indexpath value of the point directory
Nsindexpath *selectindexpath = [Nsindexpath indexpathforrow:0 insection:index];

Let table scroll to the corresponding Indexpath position
[TableView Scrolltorowatindexpath:selectindexpath Atscrollposition:uitableviewscrollpositionbottom Animated:YES];

return index;
}


Second, the table view that can be marked by row

1. Effect drawing

2. In the Cellforrow method, set the cell's accessorytype to None

Copy Code code as follows:

Define its secondary style
Cell.accessorytype = Uitableviewcellaccessorynone;

3. In the Didselectrow method
Copy Code code as follows:

Click on line Events
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
Get the cell that clicks on the row
UITableViewCell *cell = [TableView Cellforrowatindexpath:indexpath];

If the cell is already marked
if (Cell.accessorytype = = Uitableviewcellaccessorycheckmark) {
Cancel Tag
Cell.accessorytype = Uitableviewcellaccessorynone;
}

If the cell is not marked
else{
Tagged cell
Cell.accessorytype = Uitableviewcellaccessorycheckmark;
}
Uncheck effect
[TableView Deselectrowatindexpath:indexpath Animated:yes];
}

At this point, click on the line to select, uncheck, but scrolling view, you will find that some of the following has not been clicked on the line has been marked, this is because of the cell reuse mechanism caused by the first article on this issue has been mentioned

4. Solve the problem of cell reuse, in the Cellforrow method, the definition of cellidetifier, each of its lines are defined as different values, there will be no coverage, repetition and other phenomena, but this method is too rough, not the best solution, the feeling of being nasty can be used first, And then slowly debug the data on the table.

Copy Code code as follows:

NSString *cellidentifier = [NSString stringwithformat:@ "cellidentifier%d%d", indexpath.row,indexpath.section];

Three, customize table view each line of content

1. We do a similar NetEase news client News list table, the following figure left; simple effect chart, as shown

2. Data source, declared in interface

Copy Code code as follows:

Nsmutablearray *news_marray;//News content Data source
Create a new model class, named "Newsmodel", to store each data

    newsModel.h as follows, no additional code is added to. m, and if a copy is required, you can overload the Copywithzone method,
#import <foundation/ Foundation.h>
 
typedef ns_enum (Nsinteger, Newsreporttype) {
    Newsreportordinary,//General news
    newsreportexclusive,//exclusive
    newsreportspecial, //Special News
};
 
@interface newsmodel:nsobject
 
@property (nonatomic, copy) NSString *   & nbsp;   news_image;    //Picture
@property (nonatomic, copy) NSString *        news_title;    //title
@property (nonatomic, copy) NSString *        news_summary;  //Summary
@property (nonatomic, assign) nsinteger       news_replyno;  //Thread quantity
@property (nonatomic, assign) Newsreporttype reporttype ;    //coverage type
 
 
@end


In the Viewdidload method
Copy Code code as follows:

News_marray = [[Nsmutablearray alloc] init];
For (Nsinteger index =0 index<10; index++) {
Newsmodel *model = [[Newsmodel alloc] init];
Model.news_image = [NSString stringwithformat:@ "%d.jpg", index+1];
Model.news_title = @ "Once in the moonlight looking at fireworks";
Model.news_summary = @ "Had a total look at the sunset gradually lowered I how willing to put down how willing to put down";
Model.news_replyno = index+196;
Model.reporttype = index%3;

[News_marray Addobject:model];
}

3. Number of lines
Copy Code code as follows:

Number of rows per partition
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return [News_marray Count];
}

4. Custom cell controls

Before the IF (Cell==nil) in the Cellforrow method

Copy Code code as follows:

/***** Custom cell******/
Newsmodel *model = [News_marray objectAtIndex:indexPath.row];

Uiimageview * Image_view; 1. Add ImageView
Uilabel * Title_label; 2. Add Title label
Uilabel * Summary_label; 3. Add Digest Label
Uilabel * Replyno_label; 4. Add Thread Quantity Label
UIButton * Extra_view; 5. A special or exclusive report, marking
/********************/

Within the IF (Cell==nil)
Copy Code code as follows:

/***** Custom cell******/

1. Add ImageView
CGRect IMAGEVIEWF = CGRectMake (5, 5, 85, 65);
Image_view = [[Uiimageview alloc] INITWITHFRAME:IMAGEVIEWF];
[Cell Addsubview:image_view];

2. Add Title label
CGRect titlelabelf = CGRectMake (95, 5, 230, 24);
Title_label = [[Uilabel alloc] initwithframe:titlelabelf];
Title_label.font = [Uifont systemfontofsize:16];//font size
[Cell Addsubview:title_label];

3. Add Digest Label
CGRect summarylabelf = CGRectMake (97, 27, 210, 40);
Summary_label = [[Uilabel alloc] initwithframe:summarylabelf];
Summary_label.font = [Uifont systemfontofsize:12]; Font size
Summary_label.textcolor = [Uicolor Darkgraycolor]; Text color
Summary_label.numberoflines = 2;
[Cell Addsubview:summary_label];

4. Thread Quantity Label
CGRect replynolabelf = CGRectMake (210, 45, 95, 24);
Replyno_label = [[Uilabel alloc] initwithframe:replynolabelf];
Replyno_label.font = [Uifont systemfontofsize:12]; Font size
Replyno_label.textcolor = [Uicolor Darkgraycolor]; Text color
Replyno_label.textalignment = Nstextalignmentright; Align text to right

5. Thematic Extraview
CGRect EXTRAVIEWF = CGRectMake (270, 50, 28, 14);
Extra_view = [[UIButton alloc] INITWITHFRAME:EXTRAVIEWF];
Extra_view.titleLabel.font = [Uifont boldsystemfontofsize:10];
[Extra_view Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];

Regular news, only add thread number
if (model.reporttype==newsreportordinary) {
[Cell Addsubview:replyno_label];
}
Special news, add a topic logo, and add the number of threads
else if (Model.reporttype = = newsreportspecial) {

Set Background color
Extra_view.backgroundcolor = [Uicolor colorwithred:120.0/255.0 green:170.0/255.0 blue:245.0/255.0 alpha:1.0];

[Extra_view settitle:@ "exclusive" forstate:uicontrolstatenormal];//set title

[Cell Addsubview:extra_view]; Add to

Replyno_label.frame = CGRectMake (170, 45, 95, 24); Change the coordinates of the thread number label

[Cell Addsubview:replyno_label]; Add Thread Quantity Label
}
Exclusive, just add the exclusive logo
else if (Model.reporttype = = newsreportexclusive) {

Extra_view.backgroundcolor = [Uicolor Redcolor]; Set Background color

[Extra_view settitle:@ "Special topic" forstate:uicontrolstatenormal]; Set Title

[Cell Addsubview:extra_view]; Add to Cell
}
/********************/

After the IF (Cell==nil)
Copy Code code as follows:

/***** Custom cell******/
[Image_view setimage:[uiimage imagenamed:model.news_image]];//settings picture
Title_label.text = Model.news_title; Set Title
Summary_label.text = model.news_summary; Set small headings
Replyno_label.text = [NSString stringwithformat:@ "%d thread", model.news_replyno];//set the number of threads
/********************/

5. Set row height
Copy Code code as follows:

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath
{
return 75;
}

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.