因为一个项目中有大量的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. 解析HTML文件

    #!/usr/bin/env python3 # -*- coding: UTF-8 -*- from bs4 import BeautifulSoup import operator import ...

  2. Spark底层原理简化版

    目录 Spark SQL/DF的执行过程 集群运行部分 Aggregation Join Shuffle Tungsten 内存管理机制 缓存敏感计算(Cacheaware computation) ...

  3. MySQL优化小方法

    一.查询优化 1.尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引: 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而 ...

  4. Shredding Company(dfs)

    http://poj.org/problem?id=1416 题意:将一个数分成几部分,使其分割的各个数的和最大并且小于所给的数. 凌乱了..参考的会神的代码..orz... #include < ...

  5. 关于函数提升在if语句中的表现

    函数声明创建的函数在现代浏览器,在if语句中函数的声明不会提升,但是在老的IE版本中,if语句中的函数声明会提升 函数表达式在不同浏览器中函数声明都不会被提升,解决了不同浏览器的兼容性问题 关于函数提 ...

  6. SnackDown Online Pre-elimination round A

    1. 应该n是偶数,就行吧.应该判断1个人,只能出现一次吧. #include<bits/stdc++.h> #define pb push_back typedef long long ...

  7. 基于artDialog的扩展

    /* * * 引用此文件必须引用以下两个资源文件,并且还要引用jQuery * <link href="ui-dialog.css" rel="stylesheet ...

  8. IVVI SK3-02小骨酷派SK3-02 进入第三方 recovery 刷机 ROOT

    首先下载好工具:http://url.cn/5AS7IiB 备用连接 :https://pan.baidu.com/s/1jJmbYAi 本篇教程教你如何傻瓜式解锁BootLoader并进入临时rec ...

  9. 【Linux】ubuntu中怪异的vi编辑器

    由于前几天一场windows系统的比特币勒索病毒,我下狠心装了Linux的ubuntu版本.可是今天在使用命令行中的vi编辑器时出现了怪异的现象:backspace不能删除,编辑模式回车随机出现字母. ...

  10. python3:语法变动 及新特性

    python3.0 对python2.x 升级后重大语法变动,幸好留下2.7.6及后续2版本,保持一些语法兼容. 原始地址:http://hi.baidu.com/jxq61/item/3a24883 ...