Iphone custom UITableViewCell

Source: Internet
Author: User

I haven't written a blog for a long time. I have been busy developing a project recently. Now I am about to develop a project. I plan to add some tool classes I have encapsulated in the project, this article adds a custom table row. If you don't talk about it, read the code directly (the image resources used in the code will not be added ):
. H file:
[Cpp]
# Import <Foundation/Foundation. h>
 
 
@ Interface MyTableCellUtil: NSObject {
UIColor * tabletextcolor; // customize the Auto Color in tablecell
}
 
@ Property (nonatomic, retain) UIColor * tabletextcolor; // customize the Auto Color in tablecell
-(UIView *) tabcellview :( NSString *) text addImage :( UIImage *) myimage;
 
-(UIView *) tabcellview :( NSString *) text;
 
-(UIView *) tabcellview :( NSString *) text addImage :( UIImage *) myimage addusername :( NSString *) username;
 
 
@ End

. M file:
[Cpp]
# Import "MyTableCellUtil. h"
 
 
@ Implementation MyTableCellUtil
@ Synthesize tabletextcolor;
 
-(Void) dealloc {
[Tabletextcolor release];
[Super dealloc];
}
 
-(Id) init {
[Super init];
Tabletextcolor = [UIColor whiteColor];

Return self;
}
 
// Text and Image
-(UIView *) tabcellview :( NSString *) text addImage :( UIImage *) myimage {
// The total view of the row
UIView * tablecellview = [[UIView alloc] initWithFrame: CGRectZero];
Tablecellview. backgroundColor = [UIColor clearColor];
// Background image of the row
UIImage * bubble = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "2 cell" ofType: @ "png"];
UIImageView * bubbleImageView = [[UIImageView alloc] initWithImage: [bubble stretchableImageWithLeftCapWidth: 21 topCapHeight: 14];
// Text in the line
UIFont * font = [UIFont systemFontOfSize: 13];
CGSize size = [text sizeWithFont: font constrainedToSize: CGSizeMake (200366f, 1000.0f) lineBreakMode: UILineBreakModeWordWrap];

UILabel * bubbleText = [[UILabel alloc] initWithFrame: CGRectMake (110.0f, 10.0f, size. width, size. height)];
BubbleText. backgroundColor = [UIColor clearColor];
BubbleText. font = font;
BubbleText. numberOfLines = 0;
BubbleText. lineBreakMode = UILineBreakModeWordWrap;
BubbleText. text = text;
BubbleText. textColor = self. tabletextcolor;
// The image part in the row
UIImageView * oneavatarImageView = [[UIImageView alloc] initWithImage: [myimage stretchableImageWithLeftCapWidth: 21 topCapHeight: 14];

If (size. height> = 80) {// if the text height is greater than the Image height, set the cell height to the text height plus
BubbleImageView. frame = CGRectMake (0, 0,310, bubbleText. frame. size. height + 20 );
} Else {// otherwise, it is set to the height of the image plus 14, that is, 110, and the number of dead images is 96.
BubbleImageView. frame = CGRectMake (0, 0,310, 80 );
}

// Add a split line Image
UIImage * linerview = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "2 line" ofType: @ "png"];
UIImageView * lineImageView = [[UIImageView alloc] initWithImage: [linerview stretchableImageWithLeftCapWidth: 21 topCapHeight: 14];
LineImageView. frame = CGRectMake (0, bubbleImageView. frame. size. height, 320, 10 );

// Set the frame of the total view of the row
Tablecellview. frame = CGRectMake (0, 0,320, bubbleImageView. frame. size. height + lineImageView. frame. size. height );

// Set the frame of the image in the row
OneavatarImageView. frame = CGRectMake (10.0f, (bubbleImageView. frame. size. height/2)-39366f, 78.0f, 78.0f );

[BubbleImageView addSubview: oneavatarImageView];
[OneavatarImageView release];
[BubbleImageView addSubview: bubbleText];
[BubbleText release];
[Tablecellview addSubview: bubbleImageView];
[BubbleImageView release];
[Tablecellview addSubview: lineImageView];
[LineImageView release];


Return [tablecellview autorelease];
 
}
 
// Only text
-(UIView *) tabcellview :( NSString *) text {
// The total view of the row
UIView * tablecellview = [[UIView alloc] initWithFrame: CGRectZero];
Tablecellview. backgroundColor = [UIColor clearColor];
// Background image of the row
UIImage * bubble = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "2 cellTwo" ofType: @ "png"];
UIImageView * bubbleImageView = [[UIImageView alloc] initWithImage: [bubble stretchableImageWithLeftCapWidth: 21 topCapHeight: 14];
// Text in the line
UIFont * font = [UIFont systemFontOfSize: 16];
CGSize size = [text sizeWithFont: font constrainedToSize: CGSizeMake (300366f, 1000.0f) lineBreakMode: UILineBreakModeWordWrap];

UILabel * bubbleText = [[UILabel alloc] initWithFrame: CGRectMake (10.0f, 10.0f, size. width + 10, size. height + 10)];
BubbleText. backgroundColor = [UIColor clearColor];
BubbleText. font = font;
BubbleText. numberOfLines = 0;
BubbleText. lineBreakMode = UILineBreakModeWordWrap;
BubbleText. text = text;
BubbleText. textColor = self. tabletextcolor;

// Add a split line Image
UIImage * linerview = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "2 line" ofType: @ "png"];
UIImageView * lineImageView = [[UIImageView alloc] initWithImage: [linerview stretchableImageWithLeftCapWidth: 21 topCapHeight: 14];
LineImageView. frame = CGRectMake (0, bubbleText. frame. size. height + 20,320, 1 );
 

// Set the frame of the row background image
BubbleImageView. frame = CGRectMake (0, 0,320, bubbleText. frame. size. height + 20 + lineImageView. frame. size. height );

// Set the frame of the total view of the row
Tablecellview. frame = CGRectMake (0, 0,320, bubbleImageView. frame. size. height );

[BubbleImageView addSubview: bubbleText];
[BubbleText release];
[BubbleImageView addSubview: lineImageView];
[LineImageView release];
[Tablecellview addSubview: bubbleImageView];
[BubbleImageView release];
 


Return [tablecellview autorelease];
 
}
 
 
// The cellview of the Weibo itinerary
-(UIView *) tabcellview :( NSString *) text addImage :( UIImage *) myimage addusername :( NSString *) username {

 
// The total view of the row
UIView * tablecellview = [[UIView alloc] initWithFrame: CGRectZero];
Tablecellview. backgroundColor = [UIColor clearColor];
// Background image of the row
UIImage * bubble = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "2 cell" ofType: @ "png"];
UIImageView * bubbleImageView = [[UIImageView alloc] initWithImage: [bubble stretchableImageWithLeftCapWidth: 21 topCapHeight: 14];
// Text in the line
UIFont * font = [UIFont systemFontOfSize: 13];
CGSize size = [text sizeWithFont: font constrainedToSize: CGSizeMake (200366f, 1000.0f) lineBreakMode: UILineBreakModeWordWrap];

UILabel * bubbleText = [[UILabel alloc] initWithFrame: CGRectMake (110.0f, 10.0f, size. width, size. height)];
BubbleText. backgroundColor = [UIColor clearColor];
BubbleText. font = font;
BubbleText. numberOfLines = 0;
BubbleText. lineBreakMode = UILineBreakModeWordWrap;
BubbleText. text = text;
BubbleText. textColor = self. tabletextcolor;
// Www.2cto.com
UIImageView * oneavatarImageView = [[UIImageView alloc] initWithImage: [myimage stretchableImageWithLeftCapWidth: 21 topCapHeight: 14];

// Weibo publisher
UIButton * mybuttonp = [UIButton buttonWithType: UIButtonTypeCustom];
CGRect frame = CGRectMake (160, bubbleText. frame. size. height + 20,150, 20 );
Mybuttonp. frame = frame;
[Mybuttonp setTitle: [NSString stringWithFormat: @ "publisher: % @", username] forState: UIControlStateNormal];
// [Mybuttonshang addTarget: self action: @ selector (buttonViewChat) forControlEvents: UIControlEventTouchUpInside];
Mybuttonp. backgroundColor = [UIColor clearColor];
Mybuttonp. titleLabel. textColor = self. tabletextcolor;
Mybuttonp. titleLabel. font = [UIFont systemFontOfSize: 13];
// Set the frame of the background image
If (size. height + 20 + mybuttonp. frame. size. height> = 80) {// if the text height is greater than the Image height, set the cell height to the text height plus
BubbleImageView. frame = CGRectMake (0, 0,310, bubbleText. frame. size. height + 20 + mybuttonp. frame. size. height );
} Else {// otherwise, it is set to the height of the image plus 14, that is, 110, and the number of dead images is 96.
BubbleImageView. frame = CGRectMake (0, 0,310, 80 );
}



// Add a split line Image
UIImage * linerview = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "2 line" ofType: @ "png"];
UIImageView * lineImageView = [[UIImageView alloc] initWithImage: [linerview stretchableImageWithLeftCapWidth: 21 topCapHeight: 14];
LineImageView. frame = CGRectMake (0, bubbleImageView. frame. size. height, 320, 10 );

// Set the frame of the total view of the row
Tablecellview. frame = CGRectMake (0, 0,320, bubbleImageView. frame. size. height + lineImageView. frame. size. height );

// Set the frame of the image in the row
OneavatarImageView. frame = CGRectMake (10.0f, (bubbleImageView. frame. size. height/2)-39366f, 78.0f, 78.0f );

[BubbleImageView addSubview: oneavatarImageView];
[OneavatarImageView release];
[BubbleImageView addSubview: bubbleText];
[BubbleText release];
[BubbleImageView addSubview: mybuttonp];
// [Mybuttonp release];
[Tablecellview addSubview: bubbleImageView];
[BubbleImageView release];
[Tablecellview addSubview: lineImageView];
[LineImageView release];


Return [tablecellview autorelease];
 
}
@ End

Direct call (remember to set the height of each line to the height of the current custom view ):
[Cpp]
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
Static NSString * CellIdentifier = @ "Cell ";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
If (cell = nil ){
Cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
}

// NSString * cellvalue;
//// Fill in data for the first section
/// If (indexPath. section = 0 ){
// MyjsonResultUtil * myjsonUT = [self. mynotealljsonarray objectAtIndex: indexPath. section];
// If ([self. mynotealljsonarray count]> 0 ){
// OneSectionData = myjsonUT. discriptionarray;
//}
// Cellvalue = [oneSectionData objectAtIndex: indexPath. row];
// Cell. textLabel. text = cellvalue;
//// Add an arrow to the right of each row
// Cell. accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// Cell. textLabel. font = [UIFont fontWithName: @ "Verdana" size: 15];

MyjsonResultUtil * myjsonUT = [self. mynotealljsonarray objectAtIndex: indexPath. section];
If ([self. mynotealljsonarray count]> 0 ){
OneSectionData = myjsonUT. discriptionarray;
}
 
Cell. selectionStyle = UITableViewCellSelectionStyleNone;
Cell. backgroundColor = [UIColor clearColor];
UIImage * oneiavatar = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "apple" ofType: @ "png"];
MyTableCellUtil * mytablecelluitl = [[MyTableCellUtil alloc] init];
UIView * pinglunview = [mytablecelluitl tabcellview: [NSString stringWithFormat: @ "% @", [oneSectionData objectAtIndex: indexPath. row] addImage: oneiavatar];

Cell. backgroundView = pinglunview;
// [Cell. contentView addSubview: pinglunview];
[Mytablecelluitl release];
 
Return cell;
}
 
//-(NSString *) tableView :( UITableView *) tableView
// TitleForHeaderInSection :( NSInteger) section {
//}
 
 
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {
// Return 70;
UIImage * oneiavatar = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "apple" ofType: @ "png"];
MyTableCellUtil * mytablecelluitl = [[MyTableCellUtil alloc] init];
UIView * pinglunview = [mytablecelluitl tabcellview: [NSString stringWithFormat: @ "% @", [oneSectionData objectAtIndex: indexPath. row] addImage: oneiavatar];

[Mytablecelluitl release];
Return pinglunview. frame. size. height;
}

 

From RiverAM's column

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.