Iphone自定义UITableViewCell
好久没写博客了,主要是最近一段时间忙于一个开发一个工程,现在工程开发的差不多了,打算把我在工程里面封装的一些工具类添上来,这篇把自定义的表格行添出来,废话不多说,直接看代码(代码里面用到的图片资源就不添出来了):
.h文件:
[cpp]
#import <Foundation/Foundation.h>
@inte易做图ce MyTableCellUtil : NSObject {
UIColor *tabletextcolor;//自定义tablecell里面的自体颜色
}
@property(nonatomic,retain) UIColor *tabletextcolor;//自定义tablecell里面的自体颜色
- (UIView *)tabcellview:(NSString *)text addImage:(UIImage *)myimage;
- (UIView *)tabcellview:(NSString *)text;
- (UIView *)tabcellview:(NSString *)text addImage:(UIImage *)myimage addusername:(NSString *)username;
@end
.m文件:
[cpp]
#import "MyTableCellUtil.h"
@implementation MyTableCellUtil
@synthesize tabletextcolor;
-(void)dealloc{
[tabletextcolor release];
[super dealloc];
}
-(id)init{
[super init];
tabletextcolor = [UIColor whiteColor];
return self;
}
//有文字有图片
- (UIView *)tabcellview:(NSString *)text addImage:(UIImage *)myimage{
//行的总view
UIView *tablecellview = [[UIView alloc] initWithFrame:CGRectZero];
tablecellview.backgroundColor = [UIColor clearColor];
//行的背景图片
UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2cell" ofType:@"png"]];
UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
//行中的文字部分
UIFont *font = [UIFont systemFontOfSize:13];
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(200.0f, 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;
//行中的图片部分
UIImageView *oneavatarImageView = [[UIImageView alloc] initWithImage:[myimage stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
if (size.height>=80) {//如果文字的高度大于图片的高度就设置cell的高度为文字的高度加上
bubbleImageView.frame = CGRectMake(0, 0, 310, bubbleText.frame.size.height+20);
}else{//否则就设置为图片的高度加上14,也就是110,图片这里写死为96
bubbleImageView.frame = CGRectMake(0, 0, 310, 80);
}
//添加分割线图片
UIImage *linerview = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2line" ofType:@"png"]];
UIImageView *lineImageView = [[UIImageView alloc] initWithImage:[linerview stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
lineImageView.frame = CGRectMake(0, bubbleImageView.frame.size.height, 320, 10);
//设置行的总view的frame
tablecellview.frame = CGRectMake(0, 0, 320,bubbleImageView.frame.size.height+lineImageView.frame.size.height );
//设置行中图片的frame
oneavatarImageView.frame = CGRectMake(10.0f, (bubbleImageView.frame.size.height/2)-39.0f, 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];
}
//只有文字
- (UIView *)tabcellview:(NSString *)text{
//行的总view
UIView *tablecellview = [[UIView alloc] initWithFrame:CGRectZero];
tablecellview.backgroundColor = [UIColor clearColor];
//行的背景图片
UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2cellTwo" ofType:@"png"]];
UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
//行中的文字部分
UIFont *font = [UIFont systemFontOfSize:16];
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(300.0f, 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;
//添加分割线图片
UIImage *linerview = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2line" ofType:@"png"]];
UIImageView *lineImageView = [[UIImageView alloc] initWithImage:[linerview stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
lineImageView.frame = CGRectMake(0,bubbleText.frame.size.height+20, 320, 1);
//设置行背景图片的frame
bubbleImageView.frame = CGRectMake(0, 0, 320, bubbleText.frame.size.height+20+lineImageView.frame.size.height);
//设置行的总view的frame
补充:移动开发 , IOS ,