iOS development-Customizing cell-news items via Xib

Source: Internet
Author: User

Two ways to customize a cell
1> by Xib to customize the cell (if position and height are fixed)
2> to customize the cell by code

The first method: use Xib to customize the cell (drag control is convenient, but the 2 reuse identities are changed to be the same)

Change the inherited Uiviewcontroller in the controller header file to inherit Uitableviewcontroller,
Then the system automatically sets the UITableView data source and proxy as the controller, and has complied with the Protocol, probably implemented the data source method and proxy method, and the controller view is TableView, there is no view inside another tableview situation. (Self.view = = Self.tableview)
Summary: If you just want to show the table later, use this method.
Remove the view from the previous storyboard and drag a UITableView

1> Create a Xib file, drag the control
2> in the Controller implementation file Cellforrowatindexpath: Method Add Xib File
-(UITableViewCell *) TableView: (UITableView *) TableView
Cellforrowatindexpath: (Nsindexpath *) Indexpath
{
0. Defining Loop Markers
static NSString *cellidentifier = @ "News";
PS: At the same time, the cell is bound to a identifier in the Xib file for recycling and is also set to news

1. Remove the cell from the cache pool
UITableViewCell *cell = [TableView
Dequeuereusablecellwithidentifier:cellidentifier];

2. There is no cell in the cache pool
if (cell ==nil) {
Nsarray *objects = [[NSBundle Mainbundle]
loadnibnamed:@ "Newscell" Owner:nil Options:nil];
Bundle equivalent to a compressed package, passing nil defaults to Mainbundle
cell = Objects[0];
}
return cell;
}

3> Setting the cell height
Method 1: Heightforrowatindexpath by proxy method: Returns the height of the cell (if the height is not necessarily the same: Use this to return different heights depending on the line number)
Method 2: Self.tableView.rowHeight = X in viewdidload (only suitable for high-level occasions)


Package for cell
1> put the news image in the support file and create a plist file (root is an array)
2> Create a new model class news to load the plist file, declare 4 properties in News.h, and declare the constructor method and class method
3> implementation of construction methods and class methods in NEWS.M
-(ID) initwithdict: (Nsdictionary *) dict
{
if (self = [super init]) {
Parsing Dictionary Properties
Self.title = dict[@ "title"];
Self.icon = dict[@ "icon"];
Self.author = dict[@ "Author"];
self.comments = [dict[@ "comments"] intvalue];
}
return self;
}

+ (ID) newswithdict: (Nsdictionary *) dict
{
return [[Self alloc] initwithdict:dict];
}

4> Import News.h in the controller implementation file
and load the plist data in the Viewdidload
Nsarray *array = [Nsarray arraywithcontentsoffile:[nsbundle mainbundle] Pathforresource: "News.plist" ofType:nil];
PS: Declare an array member variable Nsmutablearray *newses in the class extension;
Dictionary Turn model
_newses = [Nsmutablearray array];
Iterate through all the news dictionaries in the array and convert them into models, and add them to the array above
For (nsdictionary *dict in array) {
[_newses addobject:[news newswithdict:dict];
}
}

5> Modifying the data source method
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return _news.count;
}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *)
Indexpath
{
........
3. Remove the News model
News *news = _newses[indexpath.row];
If you remove the data by binding tag, the controller knows too much and is not conducive to expansion, so it should encapsulate the cell to increase reusability.
}

Formally start the cell encapsulation
6> Create a new class Newscell to encapsulate the cell, inherit from UITableViewCell, and set the Xib file's class to Newscell.
Then declare the properties of the 4 outlet in NewsCell.h, connect the corresponding child controls, and provide a class method
+ (ID) Newscell and implement it (to create a cell)
+ (ID) Newscell
{
return [[NSBundle mainbundle loadnibnamed:@ "Newscell" Owner:nil options:nil][0];
}
7> Import NewsCell.h in the controller implementation file
Modify the data source method again (the code is now very streamlined and well-scaled)
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *)
Indexpath
{
......
1. Remove the cell from the cache pool (Newscell should be taken here)
Newscell *cell = [TableView
Dequeuereusablecellwithidentifier:cellidentifier];

2. There is no cell in the cache pool
if (cell ==nil) {
cell = [Newscell Newscell];//created here should be Newscell
}
3. Passing model data (after steps 8th and 9th)
Cell.news = _newses[indexpath.row];
The point syntax essence is the set method, so it is directly to the overridden set method

return cell;
}

8> in Newscell's header file
@class News;
@property (Nonatomic,strong) News *news//model data

9> in the Newscell implementation file
Import "News.h"
Overriding the Set method
-(void) Setnews: (News *) News
{
_news = news;

1. Title
_titlelabel.text = News.title;
2. Author
_authorlabel.text = News.author;
3. Number of comments
_commentslabel.text = [NSString stringwithformat:@ "Comments:%d", news.conments];
4. Picture
_iconview.image = [UIImage ImageNamed:news.icon];
}


The reuse of cell code
Now the controller also knows the identity of reuse, which is not good. So write a class method in the Newscell header file, return the reused identity, and then call the class method in the controller's M file.
Principle: To make any changes, the controller should not change the code. The cell is not dependent on the controller. The controller should be very idle and only responsible for passing data. There is little coupling between the controller and this cell. So when doing other projects, the cell can be used directly, so that the new controller to implement the data source method can be very simple. (Conversely, if the controller tube is too much, then cell reuse is not so easy.) )

iOS development-Customizing cell-news items via Xib

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.