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;
}
三、有图有真相
ios自己定义类(UIView)代码生成简单的UITableViewCell的更多相关文章
- lua 定义类 就是这么简单
在网上看到这样一段代码,真是误人子弟呀,具体就是: lua类的定义 代码如下: local clsNames = {} local __setmetatable = setmetatable loca ...
- iOS Developer Libray (中文版)-- Defining Classes 定义类
该篇是我自己学习iOS开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更 ...
- iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...
- iOS开发UI篇—xib的简单使用
iOS开发UI篇—xib的简单使用 一.简单介绍 xib和storyboard的比较,一个轻量级一个重量级. 共同点: 都用来描述软件界面 都用Interface Builder工具来编辑 不同点: ...
- .NET手记-定义类和接口的扩展方法
对于iOS开发者来说,使用扩展方法是家常便饭.因为有很多的类是有系统框架的定义的,我们不能修改或者不想修改他们的源码,但是我们又想要给他添加一些扩展方法来使用.这时定义扩展方法就是很有用的方式了,正如 ...
- UIKit中的几个核心对象的介绍:UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...
- Javascript定义类(class)的三种方法
将近20年前,Javascript诞生的时候,只是一种简单的网页脚本语言.如果你忘了填写用户名,它就跳出一个警告. 如今,它变得几乎无所不能,从前端到后端,有着各种匪夷所思的用途.程序员用它完成越来越 ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- JS中定义类的方法
JS中定义类的方式有很多种: 1.工厂方式 function Car(){ var ocar = new Object; ocar.color = "blue" ...
随机推荐
- Android网络连接监听
接收系统网络服务的广播接收者 public class NetStateReceiver extends BroadcastReceiver { private Handler handler; pu ...
- html5 拖放事件
1.设置元素可拖拽属性:draggable true表示可拖拽. false表示不可拖拽. auto默认值,img和带href属性的a标签则表示可拖拽,其他标签表示不可被拖拽. 其他值表示不可拖拽. ...
- Python 28 选课系统的讲解
1.首先我们要对每一个新的项目有一个明确的思路,脑子是好东西,但是好记性不如烂笔头,所以,要把能想到的都写下来 2.然后就是创建项目的整体结构框架,比如说:conf ( 配置文件 ) .core ( ...
- Wireshark 、HTTPWatch、Fiddler的介绍
一.Wireshark <TCP/IP协议监听> Wireshark(前称Ethereal)是一个网络封包分析软件.网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的网络封包 ...
- 大白话理解promise对象
Promise 代表了未来某个将要发生的事件(通常是一个异步操作) Promise 是异步编程的解决方案,能够简化多层回调嵌套,代表了未来某个将要发生的事件.Promise是一个构造函数,本身有a ...
- ie9长度兼容
onchange="this.value=this.value.substring(0, 10)" onkeydown="this.value=this.value.su ...
- span标签对于margin-top,margin-bottom无效,但是margin-left,margin-right却是有效的。
首先,span是行内元素,是没有宽高的. 我们来做个简单的布局 <div> <span>内容内容内容内容内容内容内容内容</span> </div> 页 ...
- Unity引擎GUI之Image
UGUI的Image等价于NGUI的Sprite组件,用于显示图片. 一.Image组件: Source Image(图像源):纹理格式为Sprite(2D and UI)的图片资源(导入图片后选择T ...
- AOP注解形式 整合memcache
1.首先自定义注解 :添加缓存 @Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documented@Inheritedp ...
- Day7 字符串和常用数据结构
字符串和常用数据结构 使用字符串 第二次世界大战促使了现代电子计算机的诞生,当初的想法很简单,就是用计算机来计算导弹的弹道,因此在计算机刚刚诞生的那个年代,计算机处理的信息主要是数值,而世界上的第一台 ...