1. 程式人生 > >ios自己定義類(UIView)代碼生成簡單的UITableViewCell

ios自己定義類(UIView)代碼生成簡單的UITableViewCell

implement sign eight resize nth text class += queue

因為一個項目中有大量的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