Many times, we need to customize the UITableView to meet our special requirements. At this time, there are too many customizations and techniques about UITableView and cell, which need to be summarized and summed up constantly.1. Add a custom cell. This question has been dealt with, but the main thing to say here is the comparison between the two methods! Because, I often find that there are two ways:1.xib Modethis way, in other words, add a xib file for the custom UITableViewCell class. And let the two relate. At this point, the notation is:
Return to Cell
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{
static NSString *cellidentifier = @ "MyCell";
Custom cell
MyCell *cell = (MyCell *) [Tablevie dequeuereusablecellwithidentifier:cellidentifier];
if (cell = = nil) {
This way, you will find the Xib file for the response, and the Initwithstyle method will not be called
Nsarray *array = [[NSBundle mainbundle] loadnibnamed:@ "MyCell" owner:niloptions:nil];
cell = [array objectatindex:0];
}
this way, is to read the Xib file, so, just follow the layout in the response of the xib, the layout is good, and the corresponding Initwithstyle method is not called. 2. Call the Initwithstyle method
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{
static NSString *cellidentifier = @ "MyCell";
Custom cell
MyCell *cell = (MyCell *) [Tablevie dequeuereusablecellwithidentifier:cellidentifier];
if (cell = = nil) {
This way, the Initwithstyle method in the cell will be called
cell = [[[MyCell Alloc] Initwithstyle:uitableviewcellselectionstylegray Reuseidentifier:cellidentifier] autorelease] ;
}
return cell;
}
In this way, the Initwithstyle method of the corresponding cell class is called. so, when, in that way? My understanding is:When the cell is relatively simple, the corresponding xib file can be added and associated, and when the cell is more complex, it is directly in pure code (do not create the corresponding xib file). I found that I still like to write in pure code, because, extensibility, especially when the cell element complex even with animation effect, with Xib it is difficult to control, or simply can not control. I suggest usingThe way of pure code! 2. Set the setaccessoryview Property of the cellMainly used in: Add a custom button to the right, or a child view. Set a button for Setaccessoryview.
Cell.accessorytype = Uitableviewcellaccessorynone;
UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];
[Button Setframe:cgrectmake (0.0, 0.0, 55, 57)];
[Button setimage:[uiimage imagenamed:@ "Tap_normal.png"]forstate:uicontrolstatenormal];
[Button setimage:[uiimage imagenamed:@ "Tap_highlight.png"]forstate:uicontrolstatehighlighted];
[Button SetTag:indexPath.row];
[Button addtarget:self action: @selector (doclickplaybillaction:event:) forControlEvents: UIControlEventTouchUpInside];
[Button Setbackgroundcolor:[uicolor Clearcolor];
[Cell Setaccessoryview:button];
return cell;
By observing the attribute definition:
@property (nonatomic) Uitableviewcellaccessorytype Accessorytype;
@property (Nonatomic,retain) UIView *accessoryview;
@property (nonatomic) Uitableviewcellaccessorytype Editingaccessorytype;
@property (Nonatomic,retain) UIView *editingaccessoryview;
As you can see, the Accessoryview property requires a parameter of UIView, so it's easy to customize. Of course, there are editingaccessoryview, which can be uiview when custom modifications are made.According to the user click the button, find the appropriate cell
-(void) Performexpand: (ID) paramsender{
UITableViewCell *ownercell = (uitableviewcell*) [Paramsender superview];//Gets the parent view, which is Tableviewcell
if (Ownercell! = nil) {
Nsindexpath *ownercellindexpath = [Self.mytableview Indexpathforcell:ownercell];
NSLog (@ "accessory in", index path is tapped. Index Path =%@ ", Ownercellindexpath);
}
}
3. Customize the style of cell selection.
With the previous step, we added a custom button to the cell.
Perhaps you will encounter such a tangled situation, when clicked UITableViewCell Highlight, its sub-view should not highlight the object (such as the custom button) is also highlighted.
Like what:
The correct way: when we need the cell to be selected, the button should not be highlighted. Such as:
Error mode: However, when the cell is selected, the button is highlighted. Such as:
To resolve this method, you can:
-(void) sethighlighted: (BOOL) highlighted animated: (BOOL) animated{
[Super sethighlighted:highlighted animated:animated];
if (highlighted) {
[(UIButton *) Self.accessoryview Sethighlighted:no];
}
}
-(void) setselected: (BOOL) selected animated: (BOOL) animated{
[Super setselected:selected animated:animated];
if (selected) {
[(UIButton *) Self.accessoryview Sethighlighted:no];
}
}
In this way, the problem is solved, if we take a deeper level, ask:Why are other elements in the UITableViewCell highlighted when UITableViewCell is selected?
Because when UITableViewCell is selected, UITableViewCell adds Selectedbackgroundview as a child view;
Selectedbackgroundview are added to the backgroundview of UITableViewCell, or under all other views.
When calling Setselected:animated: This method causes Selectedbackgroundview to appear and disappear in an alpha-digested state.
It should also be noted that:
Selectedbackgroundview will not work when the Selectionstyle value of UITableViewCell is Uitableviewcellselectionstylenone.
4. Add a custom background for UITableViewCellSometimes, we want to add a custom background picture for each cell of a class that UITableViewCell a custom. There are a number of ways:1. In the Initwithstyle method of the custom UITableViewCell class, add the following code:
Set background
Uiimageview *bgimage=[[[uiimageview alloc] Initwithframe:cgrectmake (0, 0,320,)] autorelease];
[Bgimage setimage: [UIImage imagenamed:@ "Table_live_bg.png"];
[Self setbackgroundview:bgimage];
2. Using the Setbackgroundimagebyname method or the SetBackgroundImage method
[Self setbackgroundimagebyname:@ "table_live_bg.png"];
[Self setbackgroundimage:[uiimage imagenamed:@ "table_live_bg.png"];
This method, to be aware, should set the picture size to be the same as the cell size 3. Set the cell's contentview, using the Insertsubview method
[Self.contentview InsertSubview:messageBackgroundViewbelowSubview:self.textLabel];
Self.selectionstyle = Uitableviewcellselectionstylenone;
these three ways are set in the Initwithstyle method. 5. Set custom text when the cell is deleted
Customizing the Delete string, adding a function to return the string to be displayed
-(NSString *) TableView: (uitableview*) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (Nsindexpath *) indexpath{
return @ "Delete";
}
This article was reproduced from: http://blog.sina.com.cn/s/blog_7b9d64af0101aaoy.html for learning reference only
"IOS" UITableView-style customizations