The strength of the UITableView is much more from the ability to customize UITableViewCell cells in any way.
Typically, the cell in UITableView is dynamic, and during use, a cell pool is created, based on the height of each cell (that is, the Tableview:heightforrowatindexpath: return value). and the screen Height calculation screen can display several cells. The custom Tableviewcell is nothing more than the implementation of code or the use of IB editing nib file to achieve two ways, the main collection of code in this article to achieve a variety of cell customization.
How to dynamically adjust cell height
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {
Staticnsstring*[email protected] "Cell";
Uitableviewcell*cell =[tableview Dequeuereusablecellwithidentifier:cellidentifier];
if (cell ==nil) {
Cell =[[[uitableviewcell alloc] Initwithframe:cgrectzero reuseidentifier:cellidentifier] autorelease];
Uilabel*label =[[uilabel alloc] Initwithframe:cgrectzero];
Label.tag = 1;
Label.linebreakmode =uilinebreakmodewordwrap;
Label.highlightedtextcolor =[uicolor Whitecolor];
Label.numberoflines = 0;
Label.opaque = no;//selected opaque indicates that nothing behind the view should be drawn
Label.backgroundcolor =[uicolor Clearcolor];
[Cell.contentview Addsubview:label];
[Label release];
}
Uilabel*label = (uilabel*) [cell viewwithtag:1];
Nsstring*text;
Text =[textarray ObjectAtIndex:indexPath.row];
CGRect cellframe =[cell frame];
Cellframe.origin =cgpointmake (0,0);
Label.text = text;
CGRect rect =cgrectinset (cellframe,2,2);
Label.frame = rect;
[Label SizeToFit];
if (Label.frame.size.height >46) {
CellFrame.size.height =50+ label.frame.size.height-46;
}
else{
CellFrame.size.height = 50;
}
[Cell Setframe:cellframe];
return cell;
}
-(CGFloat) TableView: (uitableview*) TableView Heightforrowatindexpath: (nsindexpath*) Indexpath
{
Uitableviewcell*cell =[self Tableview:tableview Cellforrowatindexpath:indexpath];
return cell.frame.size.height;
}
How to customize table Separeator split lines with pictures
In general, the use of similar [TableView Setseparatorcolor:[uicolor Redcolor]]; Statement to modify the color of the cell middle split line. So how do you use a picture as a split-line background? You can try the following:
Method One:
First set the cell Separatorcolor to clear, and then add the split line of the picture to the custom cell.
Method Two:
After adding a pixel ImageView to the cell, load the picture in, then set tableview.separatorstyle = Uitableviewcellseparatorstylenone
Customize the spacing between the first row cell and its top navigation bar
[CPP]View Plaincopy
- Tableview.tableheaderview = [[[UIView Alloc] Initwithframe:cgrectmake (0,0,5,20)] autorelease];
Customizing the accessory style of UITableViewCell
The default Accessorytype property has four values: Uitableviewcellaccessorynone, Uitableviewcellaccessorydisclosureindicator, Uitableviewcellaccessorydetaildisclosurebutton, Uitableviewcellaccessorycheckmark.
If you want to use a different style of the custom attachment button, use the Accessoryview property of the UITableView to specify
Uibutton*button;
if (Iseditableornot) {
Uiimage*image =[uiimage imagenamed:@ "Delete.png"];
Button =[uibutton Buttonwithtype:uibuttontypecustom];
CGRect frame =cgrectmake (0.0,0.0,image.size.width,image.size.height);
Button.frame = frame;
[Button Setbackgroundimage:image Forstate:uicontrolstatenormal];
Button.backgroundcolor =[uicolor Clearcolor];
Cell.accessoryview = button;
}else{
Button =[uibutton Buttonwithtype:uibuttontypecustom];
Button.backgroundcolor =[uicolor Clearcolor];
Cell.accessoryview = button;
}
The above code simply defines the style of the attachment button in both states, and the problem is that the event of this custom attachment button is still unavailable.
That is, the event is not yet delivered to the Uitableviewdelegate Accessorybuttontappedforrowwithindexpath method.
When we add the following statement in the above code:
[Button addtarget:self action: @selector (btnclicked:event:) forControlEvents: UIControlEventTouchUpInside];
After that, although you can capture the click events for each attachment button, we still can't make a difference on which line the attachment button has clicked! Because the Addtarget: method allows a maximum of two parameters to be passed: Target and event, both of which have their own purpose (target points to the event delegate object, and event points to the events that occurred). It seems that relying on the cocoa framework has not been possible.
However, we can still use the event parameter to determine which cell the event occurred on UITableView in the custom btnclicked method. Because UITableView has a critical approach Indexpathforrowatpoint, you can return the Indexpath of the cell to which the touch occurred, depending on where the touch occurred. And with the event object, you can also get the position of each touch in the view.
Check the position of the user when the button is clicked and forward the event to the corresponding accessory tapped event
-(void) btnclicked: (ID) Sender event: (ID) event
{
Nsset*touches =[event Alltouches];
Uitouch*touch =[touches Anyobject];
Cgpoint currenttouchposition =[touch LocationInView:self.tableView];
Nsindexpath*indexpath =[self.tableview Indexpathforrowatpoint:currenttouchposition];
if (Indexpath!=nil)
{
[Self TableView:self.tableView accessorybuttontappedforrowwithindexpath:indexpath];
}
}
This way,the Accessorybuttontappedforrowwithindexpath method of the UITableView is triggered and a indexpath parameter is obtained. with this indexpath parameter, we can tell which line of the attachment button has a touch event.
-(void) TableView: (uitableview*) TableView Accessorybuttontappedforrowwithindexpath: (nsindexpath*) Indexpath
{
Int*idx = Indexpath.row;
Add your own logic here.
}
Custom Uitableviewcell:cell height, split line, spacing, etc.