因为一个项目中有大量的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的更多相关文章

  1. lua 定义类 就是这么简单

    在网上看到这样一段代码,真是误人子弟呀,具体就是: lua类的定义 代码如下: local clsNames = {} local __setmetatable = setmetatable loca ...

  2. iOS Developer Libray (中文版)-- Defining Classes 定义类

    该篇是我自己学习iOS开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更 ...

  3. iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  4. iOS开发UI篇—xib的简单使用

    iOS开发UI篇—xib的简单使用 一.简单介绍 xib和storyboard的比较,一个轻量级一个重量级. 共同点: 都用来描述软件界面 都用Interface Builder工具来编辑 不同点: ...

  5. .NET手记-定义类和接口的扩展方法

    对于iOS开发者来说,使用扩展方法是家常便饭.因为有很多的类是有系统框架的定义的,我们不能修改或者不想修改他们的源码,但是我们又想要给他添加一些扩展方法来使用.这时定义扩展方法就是很有用的方式了,正如 ...

  6. UIKit中的几个核心对象的介绍:UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  7. Javascript定义类(class)的三种方法

    将近20年前,Javascript诞生的时候,只是一种简单的网页脚本语言.如果你忘了填写用户名,它就跳出一个警告. 如今,它变得几乎无所不能,从前端到后端,有着各种匪夷所思的用途.程序员用它完成越来越 ...

  8. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

  9. JS中定义类的方法

    JS中定义类的方式有很多种: 1.工厂方式    function Car(){     var ocar = new Object;     ocar.color = "blue" ...

随机推荐

  1. 杂项-Java:自定义标签

    ylbtech-杂项-Java:自定义标签 1.返回顶部 1. 一般我们说自定义标签是指JSP自定义标签.自定义标签在功能上逻辑上与javaBean 类似,都封装Java 代码.自定义标签是可重用的组 ...

  2. PCB MS SQL CLR聚合函数(函数作用,调用顺序,调用次数) CLR说明

    用CLR写函数:标量函数,表值函数 很好理解,如果用聚合函数则不是那么好理解了, 这里将CLR函数说明一下,其实关键是对聚合函数说明 用CLR写聚合函数关键点,是要理解CLR与SQL是如何进行数据交互 ...

  3. Django基础必备三件套: HttpResponse render redirect

    1. HttpResponse :  它的作用是内部传入一个字符串参数, 然后发给浏览器 def index(request): return HttpResponse('ok') 2. render ...

  4. 【Python小试牛刀】循环

    1.斐波那契数列 斐波那契数列,数列前两项为1,之后每一项都是前两项之和. #!/usr/bin/env python3 a, b = 0, 1 while b < 100: print(b) ...

  5. Redis hash结构 和常用命令

    Redis 数据结构 -- 哈希 hash 是 一个 String 类型的field 和 value 的映射表 hash 的键值 对在内存中的一种无序的状态 命令 说明 备注 hdel key fie ...

  6. 关于SSL证书配置、升级的一些问题总结

    SSL会成为网站.APP.小程序(小程序已经强制使用https)等项目的标配.关于SSL证书安装使用的问题今天总结下,以备用. 环境配置:windows server 2008 R2和IIS7.0 1 ...

  7. 【Oracle】客户端监听配置

    首先找到oracle软件安装的目录,找到\product\11.2.0\client_1\network\admin,打开tnsnames.ora文件: 粘贴一下内容: LISTENER= (DESC ...

  8. REST、RESTful、SOA

    1.http://www.imooc.com/article/17650 2.SOA面向服务架构

  9. VR: AR和VR演进哲学

    Facebook 20亿美元(4亿美元+16亿美元股票换购方式)收购虚拟现实厂商Oculus 引爆AR产业,索尼不温不火逐步演进的头盔项目也该加速了.最近Oculus rift发布了商业版本:Ocul ...

  10. 05-- C++ 类的静态成员详细讲解

     C++ 类的静态成员详细讲解    在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用.所以在所有对象中都可以共享它.使用静态成员变量实现多个对象之间的数据共享不 ...