Iphone自定义UITableViewCell

来源:未知 责任编辑:智问网络 发表时间:2013-10-22 19:17 点击:

好久没写博客了,主要是最近一段时间忙于一个开发一个工程,现在工程开发的差不多了,打算把我在工程里面封装的一些工具类添上来,这篇把自定义的表格行添出来,废话不多说,直接看代码(代码里面用到的图片资源就不添出来了):
.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的专栏

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Lexical or Preprocessor Issue 'xxx.h
    • ios学习笔记(二)xcode 4.3.2下实现基本交互
    • ios版本的helloworld
    • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
    • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
    • IOS类似iphone通讯录TableView的完整demo【附源码】
    • UITableView一些方法
    • [iPhone中级]iPhone团购信息客户端的开发 (二)
    • iphone(object-c)内存管理(1)
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1