码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-06-03 17:25:08      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:orm   syn   exp   下载   style   cts   巧克力   add   atom   

因为一个项目中有大量的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;
}

三、有图有真相

技术分享

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

标签:orm   syn   exp   下载   style   cts   巧克力   add   atom   

原文地址:http://www.cnblogs.com/zhchoutai/p/6937506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!