zoukankan      html  css  js  c++  java
  • 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 (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 contentWidthSpace_ height:contentHeightSpace_]; CGFloat labelTextHeight = [self resizeViewHeight:labelText 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;
    }

    三、有图有真相


  • 相关阅读:
    数据库(六):多表关系
    数据库(五):约束关系
    数据库(四):数据类型
    数据库(三):存储引擎
    数据库(二):初识sql语句
    数据库(一):初识数据库
    番外:socketserver用法
    ~~并发编程(十六):协程理论~~
    ~~并发编程(十五):进程池线程池~~
    ~~并发编程(十四):Queue~~
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6937506.html
Copyright © 2011-2022 走看看