Custom Uitableviewcell:cell height, split line, spacing, etc.

Source: Internet
Author: User

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 the table Separeator split line with a picture in general, use a similar [TableView Setseparatorcolor:[uicolor Redcolor]] statement to modify the color of the cell middle divider. So how do you use a picture as a split-line background? You can try this: first set the cell Separatorcolor to clear, and then add the split line of the picture to the custom cell. Method Two: Add a pixel in the cell imageview after the picture is loaded into, and then set Tableview.separatorstyle = Uitableviewcellseparatorstylenone Customizing the first row cell with its above navigation bar spacing Tableview.tableheaderview = [[[UIView alloc] initWithFrame: CGRectMake (0,0,5,20)] AutoreleasE]; The default Accessorytype property of the accessory style for custom UITableViewCell is four values: Uitableviewcellaccessorynone, Uitableviewcellaccessorydisclosureindicator, Uitableviewcellaccessorydetaildisclosurebutton, Uitableviewcellaccessorycheckmark. If you want to use a different style of the custom attachment button, you need to use the Accessoryview property of 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 for 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 event of each attachment button, we still cannot make a difference which line of 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, and so on

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.