About custom creation and custom cells for TableView (list) in iOS

Source: Internet
Author: User

Recently studied some of the basics of HTML5, some C + + foundation, some out of my iOS technology, so I have a lack of confidence in iOS recently, so today began to return to my iOS core technology, the immediate performance as a review of iOS technology, in the form of blog, write a summary, well, nonsense not much to say

Pure code creation: 1. Create TableView

2. Define a custom cell

3. Setting up the agent

4. My implementation of the Proxy method

The creation of TableView is mainly in the following steps:

1. Create TableView

-(void) Createtableview
{

Initializes the TableView and defines the position, size.
UITableView * TableView = [[UITableView alloc]initwithframe:cgrectmake (0, 0, ScreenWidth, screenheight)];

Set the data source and proxy for the table proxy for yourself
Tableview.delegate =self;
Tableview.datasource = self;

Registers the custom cell class for the table. The registration method is as follows
[TableView Registerclass:[mycell class] forcellreuseidentifier:@ "MyCell"];

Join the view.
[Self.view Addsubview:tableview];

}

2. Custom cell


@interface Mycell:uitableviewcell
@property (Nonatomic,strong) UILabel * label;
@property (Nonatomic,strong) uiimageview * image;

@end

@implementation MyCell

-(ID) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier
{

Self =[super initwithstyle:style reuseidentifier:reuseidentifier];
if (self) {
Self.label =[[uilabel alloc]initwithframe:cgrectmake (0, 0, 20, 30)];

Self.image = [[Uiimageview alloc]initwithframe:cgrectmake (20, 30, 50, 50)];

[Self.contentview AddSubview:self.image];
[Self.contentview AddSubview:self.label];

}
return self;
}

@end

3. Setting up the agent

@interface Mytableviewcontroller ()<UITableViewDelegate,UITableViewDataSource>

4. Implementation of proxy methods:

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return 15;
}


-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
{

To set the ID for cell reuse
MyCell * cell = [TableView dequeuereusablecellwithidentifier:@ "MyCell" Forindexpath:indexpath];
If the cell is not created
if (Cell==nil) {
cell= [[MyCell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@ "MyCell"];
}

Cell.textLabel.text = [NSString stringwithformat:@ "%ld", (long) indexpath.row];

return cell;

}

About custom creation and custom cells for TableView (list) in iOS

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.