ios自定义类(UIView)代码生成简单的UITableViewCell

由于一个项目中有大量的UITableViewCell需要书写,样式差不多都是 文字介绍:显示内容 这样的。自己又懒得写UITableViewCell类嫌没必要;在方法tableView:cellForRowAtIndexPath中手写又繁琐。就封装变化写了一个UIView类。

项目:点击下载

构思:首先由于文字介绍显示内容的宽度固定,然后Cell的一行(Cell可以包括多行)高度就是文字介绍显示内容所需要的高度两者相比高一些的。下一行就是高度累加重复;Cell的最上端和最下端给个高度;最下端再画个间隔。

一、UITableViewCell自定义类CommonTableViewCellView

1、CommonTableViewCellView.h

#import <UIKit/UIKit.h>

// 传递参数自动布局UITableViewCell, 样式:  lable:Value 用法:参考viewController
@interface CommonTableViewCellView : UIView{
    UIColor *_cellViewColor;// cell颜色,保留项,需要时写个方法
    CGFloat _labelSpace;// lable宽度,保留项,需要时写个方法
    CGFloat _viewHeight;
}

@property (nonatomic,retain) UIColor *cellViewColor;
@property (nonatomic,assign) CGFloat labelSpace;
@property (nonatomic,assign) CGFloat viewHeight;

- (id)initWithFrame:(CGRect)frame keyArray:(NSArray*)keyArray valueArray:(NSArray*)valueArray;
@end

2、CommonTableViewCellView.m

#import "CommonTableViewCellView.h"

#define topBottomSpace_ 10.0f
#define labelSpace_ 100.0f
#define contentWidthSpace_ self.frame.size.width - labelSpace_ - leftSpace_ - rightSpace_
#define contentHeightSpace_ 20.0f
#define leftSpace_ 20.0f
#define rightSpace_ 5.0f

@implementation CommonTableViewCellView
@synthesize cellViewColor = _cellViewColor;
@synthesize labelSpace = _labelSpace;
@synthesize viewHeight = _viewHeight;

-(void)dealloc{
    
    self.cellViewColor = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame keyArray:(NSArray*)keyArray valueArray:(NSArray*)valueArray;
{
    self = [super initWithFrame:frame];
    if (self) {
        self.labelSpace = labelSpace_;
        self.cellViewColor = [UIColor clearColor];
        
        self.viewHeight = topBottomSpace_;
        int count = keyArray.count>valueArray.count ? keyArray.count :valueArray.count;
        for (int i = 0;i < count; i++) {
            self.viewHeight = [self rectUIView:self.viewHeight labelText:[keyArray objectAtIndex:i] text:[valueArray objectAtIndex:i]];
        }
        self.viewHeight += topBottomSpace_;
        // 横 分割线
        UIImageView *imgView_H = [[UIImageView alloc]initWithFrame:CGRectMake( 0, self.viewHeight-1, self.frame.size.width, 1)];
        imgView_H.backgroundColor = [UIColor colorWithRed:221/255.0f green:221/255.0f blue:221/255.0f alpha:1.0];
        [self addSubview:imgView_H];
        [imgView_H release];

        [self setFrame:CGRectMake(0, frame.origin.y, self.frame.size.width, self.viewHeight)];
    }
    return self;
}


// 重置高度
-(CGFloat)resizeViewHeight:(NSString *)text width:(CGFloat)width height:(CGFloat)height{
    
    CGSize constraint = CGSizeMake(width, 2000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    return size.height>height?size.height:height;
}

// 行
-(CGFloat)rectUIView:(CGFloat)height labelText:(NSString*)labelText text:(NSString*)text{
    
    CGFloat textValueHeight = [self resizeViewHeight:text width:contentWidthSpace_ height:contentHeightSpace_];
    CGFloat labelTextHeight = [self resizeViewHeight:labelText width:self.labelSpace height:contentHeightSpace_];
    CGFloat cellHeight = textValueHeight>labelTextHeight ? textValueHeight : labelTextHeight;
    
    UILabel *label = [self rectUILabel:labelText rect:CGRectMake(leftSpace_, height, self.labelSpace, cellHeight)];
    [self addSubview:label];
    
    UILabel *textValueLabel = [self rectUILabel:text rect:CGRectMake(self.labelSpace + leftSpace_, height, contentWidthSpace_, cellHeight)];
    [self addSubview:textValueLabel];
    
    return height + cellHeight ;
}

// 列
- (UILabel *)rectUILabel:(NSString *)text rect:(CGRect)rect{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.backgroundColor = self.cellViewColor;
    label.textAlignment = UITextAlignmentLeft;
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:13.0];
    label.text = text;
    return [label autorelease];
}

@end

二、UIViewController调用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    CGFloat height = cell.frame.size.height;
    return height;
}

- (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];
        
        UIView *view = [[[UIView alloc] init] autorelease];
        CGFloat viewHeight = 0.0f;
        for (int i=0 ; i < 5 ; i++) {
            
            NSMutableArray *keyArray = [NSMutableArray arrayWithObjects:@"文字介绍1:",@"文字介绍2:",@"知道你过得不好 我也就安心了3:", nil];
            NSMutableArray *valueArray = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"随机数据%d", arc4random_uniform(100)],[NSString stringWithFormat:@"生活就像一盒巧克力 你永远不知道你会得到什么%d", arc4random_uniform(100)],[NSString stringWithFormat:@"随机数据%d", arc4random_uniform(100)], nil];

            CommonTableViewCellView *cellView = [[[CommonTableViewCellView alloc] initWithFrame:CGRectMake(0, viewHeight, self.view.frame.size.width, 0) keyArray:keyArray valueArray:valueArray] autorelease];
            viewHeight += cellView.viewHeight;
            [view addSubview:cellView];
        }
        [view setFrame:CGRectMake(0, 0, self.view.frame.size.width, viewHeight)];
        cell.accessoryView = view;
        [cell setFrame:CGRectMake(0, 0, self.view.frame.size.width, viewHeight)];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return cell;
}

三、有图有真相


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。