Custom UITableViewCell: Cell height, split line, spacing, etc,

Source: Internet
Author: User

Custom UITableViewCell: Cell height, split line, spacing, etc,

UITableView is more powerful than custom UITableViewCell cells.

Generally, cells in UITableView are dynamic. during use, a Cell pool is created based on the height of each Cell (tableView: heightForRowAtIndexPath: return value ), and several cells can be displayed on the screen height computing screen. The custom TableViewCell is implemented only by code implementation or by using IB to edit the nib file. This article mainly collects code to implement various cell customization methods.

How to dynamically adjust Cell height

 

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {

StaticNSString * CellIdentifier = @ "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; // If Opaque is selected, NO content 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 use images to customize Table Separeator split lines
Generally, the color of the split line in the cell can be modified using statements similar to [tableView setSeparatorColor: [UIColor redColor. Then how can we use an image as the background of the split line? Try the following:
Method 1:
Set the cell separatorColor to clear, and then add the split line of the image to the custom M cell.

Method 2:
Add a pixel imageView to the cell, load the image into it, and set tableView. separatorStyle = UITableViewCellSeparatorStyleNone.

Customize the spacing between the Cell on the first line and the preceding navigation bar

  1. TableView. tableHeaderView = [[[UIView alloc] initWithFrame: CGRectMake (0, 0, 5, 20)] autorelease];

Customize the accessory style of UITableViewCell
The default accessoryType attribute has four values: UITableViewCellAccessoryNone, UITableViewCellAccessoryDisclosureIndicator, UITableViewCellAccessoryDetailDisclosureButton, and UITableViewCellAccessoryCheckmark.

If you want to use other styles of the custom attachment button, you must use the accessoryView attribute 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 only defines the style of the attachment button in two states. The problem is that the event of the custom attachment button is still unavailable.

That is, the event cannot be passed to the accessoryButtonTappedForRowWithIndexPath method of UITableViewDelegate.

When we add the following statement in the above Code:
[Button addTarget: self action: @ selector (btnClicked: event :) forControlEvents: UIControlEventTouchUpInside];
Even though the click event of each attachment button can be captured, we cannot identify which row of the attachment button has been clicked! Because addTarget: The method can pass a maximum of two parameters: target and event, both of which have their own purposes (target points to the event Delegate object, and event points to the event ). It seems that it is impossible to rely on the Cocoa framework.

However, we can still use the event parameter to determine which cell of UITableView the event occurred in the Custom btnClicked method. Because UITableView has a key method indexPathForRowAtPoint, you can return the indexPath of the cell where the touch occurs based on the position where the touch occurs. In addition, you can use the event object to obtain the position of each touch in the view.

 

// Check the location when the user clicks the button 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];

}

}

 

In this way, the accessoryButtonTappedForRowWithIndexPath method of UITableView is triggered and an indexPath parameter is obtained. With this indexPath parameter, we can tell which row of the attachment button has a touch event.

 

-(Void) tableView :( UITableView *) tableView accessoryButtonTappedForRowWithIndexPath :( NSIndexPath *) indexPath

{

Int * idx = indexPath. row;

// Add your own logic here

}


IOS TableViewCell custom split line

The product design requires that the cell split line length not be the whole screen width, and the design requires that the split line is 2px (two), with different colors on and off. Implementation: Change the split line style to None tableView in UITableView. separatorStyle = response; custom UITableViewCell write-(void) drawRect :( CGRect) rect method-(void) drawRect :( CGRect) rect {CGContextRef context = Response (); context (context, [UIColor clearColor]. CGColor); CGContextFillRect (context, rect); // upper split line, CGContextSetStrokeColorWithColor (context, [UIColor colorWithHexString: @ "ffffff"]. CGColor); CGContextStrokeRect (context, CGRectMake (5,-1, rect. size. width-10, 1); // CGContextSetStrokeColorWithColor (context, [UIColor colorWithHexString: @ "e2e2e2e2"]. CGColor); CGContextStrokeRect (context, CGRectMake (5, rect. size. height, rect. size. width-10, 1 ));}

How to set the height of UITableViewCell?

-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath; in this function, judge and set the height to the DEVDIV. COM website to view the answer details>
 

Related Article

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.