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" ...
随机推荐
- Appium + python - 监控appium server start
import osimport time as t def start_appium(port = 4723,udid="4871660c"): a = os.popen(&quo ...
- flume+flume+kafka消息传递+storm消费
通过flume收集其他机器上flume的监测数据,发送到本机的kafka进行消费. 环境:slave中安装flume,master中安装flume+kafka(这里用两台虚拟机,也可以用三台以上) m ...
- netty之ByteBuf详解
[ChannelPromise作用:可以设置success或failure 是为了通知ChannelFutureListener]Netty的数据处理API通过两个组件暴露——abstract cla ...
- MessagePack 新型序列化反序列化方案
进入在学习redis的时候,在文中看到了关于MessagePack的简介,发现非常有意思,于是就花了点时间大致了解了下. MessagePack介绍: MessagePack is an effici ...
- angular的directive指令的link方法
比如 指令标签 <mylink myLoad="try()"></mylink> link:function(scope,element,attr){ el ...
- TCP/IP详解(三)
超时与重传: TCP在发送一个包时,启动一个定时器,如果在定时器溢出之前没有收到ACK,则认为发出的包丢失了,此时会重传丢失的包.这就是超时重传. 其中定时器的时间不是一个固定值,它是根据RTT计算的 ...
- R - Games
Problem description Manao works on a sports TV. He's spent much time watching the football games of ...
- D - Garden
Problem description Luba thinks about watering her garden. The garden can be represented as a segmen ...
- 努比亚 Z17(Nubia NX563J) 解锁BootLoader 并刷入recovery
工具下载链接:https://pan.baidu.com/s/1mjEzcyG 备用下载链接:https://pan.baidu.com/s/1eTdx6Zg 密码:1d3r 本篇教程教你如何傻瓜式解 ...
- [PHP][学习笔记][CURL]监测设备运行情况小demo
1.curl获取的web content 不能直接echo到页面,会造成js各种错误 2.想办法处理字符串的截取.拼接 2.1.裁剪html返回的字符串 function cutStringFrom( ...