好久沒寫部落格了,主要是最近一段時間忙於一個開發一個工程,現在工程開發的差不多了,打算把我在工程裡面封裝的一些工具類添上來,這篇把自訂的表格行添出來,廢話不多說,直接看代碼(代碼裡面用到的圖片資源就不添出來了):
.h檔案:
[cpp]
#import <Foundation/Foundation.h>
@interface 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
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];
}
//行程微博的cellview
- (UIView *)tabcellview:(NSString *)text addImage:(UIImage *)myimage addusername:(NSString *)username{
//行的總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;
//行中的圖片部分 www.2cto.com
UIImageView *oneavatarImageView = [[UIImageView alloc] initWithImage:[myimage stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
//發表微博的人
UIButton *mybuttonp = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(160, bubbleText.frame.size.height+20, 150, 20);
mybuttonp.frame = frame;
[mybuttonp setTitle:[NSString stringWithFormat:@"發表人:%@",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];
//設定背景圖片的frame
if (size.height+20+mybuttonp.frame.size.height>=80) {//如果文字的高度大於圖片的高度就設定cell的高度為文字的高度加上
bubbleImageView.frame = CGRectMake(0, 0, 310, bubbleText.frame.size.height+20+mybuttonp.frame.size.height);
}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];
[bubbleImageView addSubview:mybuttonp];
// [mybuttonp release];
[tablecellview addSubview:bubbleImageView];
[bubbleImageView release];
[tablecellview addSubview:lineImageView];
[lineImageView release];
return [tablecellview autorelease];
}
@end
調用的時候直接(記得設定每行的高度為當前自訂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;
// //為第一個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;
// //為每一行右邊添加箭頭
// 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;
}
摘自 RiverAM的專欄