自定义tableviewcelll的元素不确定,动态改变时,怎么了自定义cell

二次元同好交流新大陆
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
转自&不知道大家有没有发现,在iOS APP开发过程中,UITableView是我们显示内容常见的控件,本人觉得它是UIKit中最复杂的一个控件。今天要向大家介绍的就是如何动态计算UITableViewCell高度的一经验与技巧,在此做一些总结方便朋友们查阅。为了不让讲解空洞抽象,我还是用代码实例的方式进行讲解,这样更容易接收与学习。&本文将介绍四种情况下UITableViewCell的计算方式,分别是:1. Auto Layout with UILabel in UITableViewCell2. Auto Layout with UITextView in UITableViewCell3. Manual Layout with UILabel in UITableViewCell4. Manual Layout with UITextView in UITableViewCell5. 随UITextView高度动态改变Cell高度&首先创建一个Single Page的工程,我命名为CellHeightDemo&1. Auto Layout with UILabel in UITableViewCell创建一个空的xib,命名为C1.xib, 然后拖入一个UITableViewCell控件。接着创建一个UITableViewCell的子类,命名为C1类。然后在C1.xib中,将与C1类进行关联。别给我说你不会关联,如果不会那看下图你就明白了。&&只需要在Class那里写入关联的类名C1即可。&还有由于UITableViewCell需要重用功能,所以我们还需要设置一个重用标识&&在Identifier那里写入重用标识C1,当然你也可以用任意字符。不过后面代码里需要这个字符。&接着我们来布局。用到了auto layout, 在此我不想介绍auto layout, 以后有时间再专门介绍,下图就是我布局&&这儿有两点需要说明:1. UILabel的属性Lines这儿设为了0表示显示多行。2. Auto Layout一定要建立完完整。&接着我们在UITableView中来使用我们自定义的UITableViewCell C1。&首先我们创建一个UITableViewController的子类T1ViewController, 接着在Main.storyboard中拖入一个UITableViewController,并关联T1ViewController。&一切都准备好了,那我们现在来写点代码,给UITableView加点料。&我们想要我们的UITableView使用C1.xib中自定义的Cell,那么我们需要向UITableView进行注册。&这样就进行注册了,接着我们还需要每行显示的数据,为了简单一点,我就声明了一个NSArray变量来存放数据。&现在实现UITableViewDataSource的protocol:&从self.tableData中的数据我们可以看到,每一个Cell显示的数据高度是不一样的,那么我们需要动态计算Cell的高度。由于是auto layout,所以我们需要用到一个新的API systemLayoutSizeFittingSize:来计算UITableViewCell所占空间高度。Cell的高度是在- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath这个UITableViewDelegate的方法里面传给UITableView的。&这里有一个需要特别注意的问题,也是效率问题。UITableView是一次性计算完所有Cell的高度,如果有1W个Cell,那么- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath就会触发1W次,然后才显示内容。不过在iOS7以后,提供了一个新方法可以避免这1W次调用,它就是- (CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath。要求返回一个Cell的估计值,实现了这个方法,那只有显示的Cell才会触发计算高度的protocol. 由于systemLayoutSizeFittingSize需要cell的一个实例才能计算,所以这儿用一个成员变量存一个Cell的实列,这样就不需要每次计算Cell高度的时候去动态生成一个Cell实例,这样即方便也高效也少用内存,可谓一举三得。&我们声明一个存计算Cell高度的实例变量:&然后初始化它:&下面是计算Cell高度的实现:&看了代码,可能你有点疑问,为何这儿要加1呢?笔者告诉你,如果不加1,结果就是错误的,Cell中UILabel将显示不正确。原因就是因为这行代码CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];由于是在cell.contentView上调用这个方法,那么返回的值将是contentView的高度,UITableViewCell的高度要比它的contentView要高1,也就是它的分隔线的高度。如果你不相信,那请看C1.xib的属性,比较下面两张图。&&&发现没Cell的高度是127, 面contentView的高度是126, 这下明白了吧。&为了让读者看清楚,我将Cell中UILabel的背景色充为了light gray.下面是运行效果:&&2. Auto Layout with UITextView in UITableViewCell本小段教程将介绍UITextView在cell中计算高度需要注意的地方。同样参考上面我们创建一个C2.xib, UITableViewCell的子类C2,并关联C2.xib与C2类。并在C2.xib中对其布局,同样使用了auto layout. 布局如下图:&&创始UITableViewController的了类T2ViewController,在Main.storyboard中拖入UITableViewController,并关联他们。接着代码中注册C2.xib到UITableView.&下面计是计算高度的代码:&在这儿我们是通过sizeThatFits:计算的UITextView的高度(这是计算UITextView内容全部显示时的方法,在第四小段中我们还会用到它),然后加上systemLayoutSizeFittingSize:返回的高度。为什么要这样呢? 因为UITextView内容的高度不会影响systemLayoutSizeFittingSize计算。这句话什么意思呢?我真不知道如何用言语表达了。还是先上一张图吧:&&此图中距顶的约束是10, 距底的约束8, 距左边约束是87,距右边的约束是13, 那么systemLayoutSizeFittingSize:返回的CGSize为height等于19, size等于100. 它UITextView的frame是不影响systemLayoutSizeFittingSize:的计算。不知道这样说大家明白没。所以,我们需要加上textViewSize.height.&&下面是运行效果:&&3. Manual Layout with UILabel in UITableViewCell本小段教程将介绍UILabel在Manual layout cell中计算高度, 原理是根据字体与字符串长度来计算长度与宽度。 按照前面介绍的,我们需要创建C3.xib, C3类, T3ViewController类,Main.storyboard中拖入UITableViewController,并分别建立关联。 为了简单,C3.xib中我就不加padding之类的了,如图&记得关闭C3.xib的auto layout&&直接上代码了:&这儿用到了一个NSString的Cagetory方法:&原理上面我已说了,这儿没有什么好说明的,代码一目了然。&运行效果如图:&&4. Manual Layout with UITextView in UITableViewCell本小段教程将介绍UITextView在Manual layout cell中计算高度, 原理是与第二小节里的相同,用sizeThatFits:的方法计算UITextView的长度与高度。然后加上padding就是Cell的高度。 按照前面介绍的,我们需要创建C4.xib, C4类, T4ViewController类,Main.storyboard中拖入UITableViewController,并分别建立关联。 为了简单,C4.xib中我就不加padding之类的了,如图&记得关闭C4.xib的auto layout&&也直接上代码了,直观明了:&运行效果:&&5. 随UITextView高度动态改变Cell高度本小节要介绍的一个功能是,UITextView中UITableViewCell中,当输入UITextView中的字变多/变少时,高度变化,Cell高度与随之变化的功能。按照前面介绍的,我们需要创建C5.xib, C5类, T5ViewController类,Main.storyboard中拖入UITableViewController,并分别建立关联。 为了简单,C5.xib中我就不加padding之类的了,如图&记得开启C5.xib的auto layout&&先看代码:&原理就是UITextView内容改变的时候,计算自身高度,然后通知UITableView更新,这样就会触发UITableViewCell高度重新计算,以达到目的。&&本文只是简单的介绍了一些原理与技巧,细节之处还请参看&参考:&&&&CocoaChina是全球最大的苹果开发中文社区,官方微信每日定时推送各种精彩的研发教程资源和工具,介绍app推广营销经验,最新企业招聘和外包信息,以及Cocos2d引擎、Cocos Studio开发工具包的最新动态及培训信息。关注微信可以第一时间了解最新产品和服务动态,微信在手,天下我有!请搜索微信号“CocoaChina”关注我们!
阅读(309)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'动态设定TableView cell的高度',
blogAbstract:'动态计算UITableViewCell高度详解',
blogTag:'ios',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:9,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'三无产品',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}今天看啥 热点:
iOS_11_tableViewCell的使用alertView修改数据,ios自定义alertview
最终效果图:
11_tableView的使用_红楼梦
Created by beyond on 14-7-26.
Copyright (c) 2014年 com.beyond. All rights reserved.
#import &Foundation/Foundation.h&
@interface Girl : NSObject
// UI控件用weak,字符串用copy,其他对象用strong
// 头像图片名
@property(nonatomic,copy)NSString *headImgN
@property(nonatomic,copy)NSString *
@property(nonatomic,copy)NSString *
// 提供一个类方法,即构造函数,返回封装好数据的对象(返回id亦可)
+ (Girl *)girlNamed:(NSString *)name headImgName:(NSString*)headImgName verdict:(NSString *)
11_tableView的使用_红楼梦
Created by beyond on 14-7-26.
Copyright (c) 2014年 com.beyond. All rights reserved.
#import &Girl.h&
@implementation Girl
// 提供一个类方法,即构造函数,返回封装好数据的对象(返回id亦可)
+(Girl *)girlNamed:(NSString *)name headImgName:(NSString *)headImgName verdict:(NSString *)verdict
Girl *girl = [[Girl alloc]init];
girl.name =
girl.headImgName = headImgN
girl.verdict =
BeyondViewController.h
BeyondViewController.h
11_tableView的使用_红楼梦
Created by beyond on 14-7-26.
Copyright (c) 2014年 com.beyond. All rights reserved.
#import &UIKit/UIKit.h&
@interface BeyondViewController : UIViewController
// 重新刷新表格时要用到
@property (weak, nonatomic) IBOutlet UITableView *tableV
BeyondViewController.m
BeyondViewController.m
11_tableView的使用_红楼梦
Created by beyond on 14-7-26.
Copyright (c) 2014年 com.beyond. All rights reserved.
#import &BeyondViewController.h&
#import &Girl.h&
@interface BeyondViewController ()&UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate&
// 存放假数据
NSMutableArray *_
@implementation BeyondViewController
- (void)viewDidLoad
[super viewDidLoad];
_array = [NSMutableArray array];
[_array addObject:[Girl girlNamed:@&林黛玉& headImgName:@&0.png& verdict:@&可叹停机德,堪怜咏絮才。玉带林中挂,金簪雪里埋。&]];
[_array addObject:[Girl girlNamed:@&薛宝钗& headImgName:@&1.png& verdict:@&可叹停机德,堪怜咏絮才。玉带林中挂,金簪雪里埋。&]];
[_array addObject:[Girl girlNamed:@&妙玉& headImgName:@&2.png& verdict:@&欲洁何曾洁,云空未必空。可怜金玉质,终陷淖泥中。&]];
[_array addObject:[Girl girlNamed:@&史湘云& headImgName:@&3.png& verdict:@&富贵又何为?襁褓之间父母违。展眼吊斜辉,湘江水逝楚云飞。&]];
[_array addObject:[Girl girlNamed:@&探春& headImgName:@&4.png& verdict:@&才自清明志自高,生于末世运偏消。清明涕泣江边望,千里东风一梦遥。&]];
[_array addObject:[Girl girlNamed:@&惜春& headImgName:@&5.png& verdict:@&堪破三春景不常,缁衣顿改昔年妆。可怜秀户侯门女,独卧青灯古佛旁。&]];
[_array addObject:[Girl girlNamed:@&王熙凤& headImgName:@&6.png& verdict:@&凡鸟偏从末世来,都知爱慕此生才。一从二令三人木,哭向金陵事可哀。 &]];
#pragma mark - tableView的数据源方法
// 数据源方法,特例,重要~ 一共有多少个分组 (默认就是返回1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// 单组数据显示,无需分组,故返回 1,(默认就是返回1)
// 数据源方法,每一组,有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// 数据源方法,每一组的每一行应该显示怎么的界面(含封装的数据),重点!!!
// 每当有一行cell 进入视野范围就会调用
// 必须实现否则,Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellID = @&Beyond&;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
// 如果池中没取到,则重新生成一个cell
cell的4种样式:
左图右文字
2,subtitle
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
// 设置cell中独一无二的内容
Girl *girl = _array[indexPath.row];
cell.imageView.image = [UIImage imageNamed:girl.headImgName];
cell.textLabel.text = girl.
cell.detailTextLabel.text = girl.
// 设置单元的右边附属
// cell.accessoryType = UITableViewCellAccessoryDetailB
// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureB
cell.accessoryType = UITableViewCellAccessoryDisclosureI
// 返回cell
#pragma mark - tableView的代理方法
// 代理方法,每一行多高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 93;
// 代理方法,将要点击某一行的时候调用
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@&will select----%d&,indexPath.row);
return indexP
// 代理方法,当点击一行时调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@&did select----%d&,indexPath.row);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Girl *girl = _array[indexPath.row];
// 弹出姓名,以供用户更改
// 设置代理的目的是响应alert的按钮点击事件
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:girl.name message:girl.verdict delgate:self cancelButtonTitle:@&Cancel& otherButtonTitles:@&other&, nil];
UIAlertView *alert = [[UIAlertView alloc]init];
[alert initWithTitle:girl.name message:girl.verdict delegate:self cancelButtonTitle:@&取消& otherButtonTitles:@&确定&, nil];
// alertViewStyle 样式----密码
// alert.alertViewStyle = UIAlertViewStyleSecureTextI
// alertViewStyle 样式----一般的文本输入框
alert.alertViewStyle = UIAlertViewStylePlainTextI
// alertViewStyle 样式----用户名及密码登录框
// alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordI
// alertViewStyle 样式----标签显示
// alert.alertViewStyle = UIAlertViewStyleD
// 用户名密码的情况下有两个文本框
UITextField *textField = [alert textFieldAtIndex:0];
textField.text = girl.
// 关键代码,通过tag将点击的行号带给alertView的代理方法,还可以通过利用代理即控制器的成员进行 行号 的传递~
textField.tag = indexPath.
// 显示alertView
[alert show];
默认情况下,上面的alert是局部变量,在本方法调完的时候,会被释放
但是,[alert show]方法,会有一种机制(比如UIWindow会持有它的引用,使之不被销毁)
// 代理方法,当取消点击一行时调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@&did deselect row----%d&,indexPath.row);
#pragma mark - UIAlertViewDelegate的代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
// 查看点击了alertView里面的哪一个按钮,取消按钮是 0
NSLog(@&alertView里面的按钮index---%d&,buttonIndex);
if (buttonIndex == 0) {
// 0代表取消按钮
}else if (buttonIndex == 1){
// 1代表确定按钮,更新数据源,重新加载数据
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *newName = [textField text];
// robust判断
if ([newName isEqualToString:@&&]) {
// 先更新数据源
int row = textField.
Girl *girl = _array[row];
girl.name = newN
// 再,全部重新加载数据
// [_tableView reloadData];
// 最好是,局部刷新数据,通过row生成一个一个indexPath组成数组
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
NSArray *indexPaths = @[indexPath];
[_tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
tableViewCellAccessory
tableViewCellStyle
tableView数据源
首先,这个功能是不用反复测试的。然后,就算测试,建议虚拟机,真机测试需要改源码,而且是机器运行的源码,不是你自己写的源码。因为这个设置是一次性设置,设置后就会返回值到沙箱里了,再也不改了。虚拟机的话每次退出都是一次重装,so,强推虚拟机测试!另外,虚拟机如果出现同样情况,就进电脑的开发者文件把沙箱内容都给删了,那里只是临时文件,因为运行才产生的文件,尽管删,不怕的。再然后,还是给你说,不需要反复测试的。。。
UI是在主线程操作的,所以你可以在主线程去弹出alert,然后后台线程去做其他的,同时捕获后台线程的数据
相关搜索:
相关阅读:
相关频道:
Android教程最近更新iOS开发UITableView 之代码自定义cell的步骤
虽然在UITableView中可以直接拖控件的方式进行添加cell,但是这种方式有一个致命的缺点,那就是cell是固定的,而且cell的高度难以更改。在实际的开发中并不能满足我们的需求。比如以下:
wps_clip_image-16424
在这个TableView中每一个cell中有一个显示时间的label,一个显示内容的button,一个显示头像的imageView 并且由于聊天内容的多少 每一个cell的高度都是动态改变的,显然系统提供的cell并不能满足需求!
与此类似的还有微博手机客户端,糗事百科客户端等等
以下是搭建此类UI界面的大概步骤,不涉及网络功能,和存储功能,所有的数据信息用plist文件配置。
wps_clip_image-8661
1.1 根据数据建立数据模型
@interface CLWeiBo : NSObject
@property (nonatomic,copy) NSString *//正文
@property (nonatomic,copy) NSString *//头像
@property (nonatomic,copy) NSString *//昵称
@property (nonatomic,copy) NSString *//图片
@property (nonatomic,assign,getter = isVip) BOOL *//VIP
1.2 提供两个初始化方法
- (instancetype) initWithDict: (NSDictionary *)//对象方法
+ (instancetype) weiboWithDict: (NSDictionary *)//类方法
方法内部实现就不再赘述了!
2.1 根据数据模型建立frame模型
之所以建立frame模型,是为了封装计算cell内各个控件位置的过程。这体现了OOP的思想!
在Frame模型中,各个控件的Frame会根据传递过来的数据模型进行计算设置,所以Frame模型中必须有一个数据模型属性,且计算各个控件的frame的过程应该放在模型数据属性的set方法中,并且在这里计算出cell的高度
@interface CLWeiBoFrame : NSObject
/** &* &头像的Frame 如果成员变量只允许类本身修改,应该把它声明成readonly,体现良好的编码习惯。 &*/
@property (nonatomic, assign, readonly)CGRect iconF;
/** &* &正文的frame &*/
@property (nonatomic, assign, readonly)CGRect textF;
/** &* &VIP图标的frame &*/
@property (nonatomic, assign, readonly)CGRect vipF;
/** &* &昵称的Frame &*/
@property (nonatomic, assign, readonly)CGRect nameF;
/** &* &图片的Frame &*/
@property (nonatomic, assign, readonly)CGRectpictureF;
/** &* &数据模型 &*/
@property (nonatomic, strong) CLWeiBo *
/** &* &cell的高度 &*/
@property (nonatomic, assign, readonly) CGFloat cellH
3.1 新建CLWeiBocell并且继承自UITableViewCell
有frame模型类型的成员属性,用来对cell内的各个控件设置frame 以及数据
因为frame模型中有一个数据模型 所以这里无需再引入数据模型
@property (nonatomic, strong) CLWeiBoFrame *weiboF
并且提供一个类方法,外界调用建立cell的接口,需要传入参数tableView &。传入tableView参数是为了能够取得tableView内的cell队列循环利用cell
+ (instancetype)cellWithTableView:(UITableView*)tableView
{ & & static NSString *ID = @&weibo&; & & CLWeiBoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; & & if (cell == nil) { & & & & cell = [[CLWeiBoCell &alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; & & } & &
3.2 重写的cell的构造方法 在初始化cell对象的时候调用,在这个方法中添加cell的子控件
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithStyle:stylereuseIdentifier:reuseIdentifier];
if (self) { & & & &&
// 1.头像 &
//在这里只是声明并添加控件 控件的frame设置以及数据的设置放在frame模型属性的set方法中 在拿到frame模型数据后同时设置cell内子控件的frame和数据 & & & &&
UIImageView *iconView = [[UIImageView alloc]init]; & & & &&
[self.contentView addSubview:iconView]; & & & &&
self.iconView = iconV & & & & & & & & &
// 2.昵称 & & & &&
UILabel *nameView = [[UILabel alloc] init]; & & & &&
nameView.font = MJNameF & & & &&
[self.contentView addSubview:nameView]; & & & &&
self.nameView = nameV & & & & & & & & &
// 3.会员图标 & & & &&
UIImageView *vipView = [[UIImageView alloc]init]; & & & &&
vipView.image = [UIImage imageNamed:@&vip&]; & & & &&
[self.contentView addSubview:vipView]; & & & &&
self.vipView = vipV & & & & & & & & &
// 4.正文 & & & &&
UILabel *textView = [[UILabel alloc] init]; & & & &&
textView.numberOfLines = 0; & & & &&
textView.font = MJTextF & & & &&
[self.contentView addSubview:textView]; & & & &&
self.textView = textV & & & & & & & & &
// 5.配图 & & & &&
UIImageView *pictureView = [[UIImageViewalloc] init]; & & & &&
[self.contentView addSubview:pictureView]; & & & &&
self.pictureView = pictureV & & } & &&
//frame模型数据 变量的set方法 在这里接到外界的frame模型数据的同时设置frame和显示数据
- (void) setCLWeiBoFrame:(MJStatusFrame *)statusFrame
_statusFrame = statusF & & & & &
// 1.设置数据
& & [self settingData]; & & & & &
// 2.设置frame
& & [self settingFrame];
4.新建控制器继承UITableViewController 并且遵守UITableView数据源协议
4.1 在控制器中声明frame模型属性数组
/** &* &存放所有cell的frame模型数据 &*/
@property (nonatomic, strong) NSArray *weiboF
//在weiboFrames的set方法中完成plist文件中的字典数据向模型数据转换的过程
- (NSArray *)statusFrames
if (_weiboframes == nil)&
{ & & & &&
// 初始化 & & & &&
// 1.获得plist的全路径 & & & &&
NSString *path = [[NSBundle mainBundle]pathForResource:@&statuses.plist& ofType:nil]; & & & & & & & & &
// 2.加载数组 & & & &&
NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path]; & & & & & & & & &
// 3.将dictArray里面的所有字典转成模型对象,放到新的数组中 & & & &&
NSMutableArray *weibosFrameArray = [NSMutableArray array]; & & & &&
for (NSDictionary *dict in dictArray) { & & & & & &&
// 3.1.创建CLWeiBo模型对象 & & & & & &&
CLWeiBo *weibo = [CLWeiBo weiboWithDict:dict]; & & & & & & & & & & & & &
// 3.2.创建CLWeiBoFrame模型对象 & & & & & &&
CLWeiBoFrame *weiboFrame = [[CLWeiBoFrame alloc] init]; & & & & & &&
weiboFrame.weibo = & & & & & & & & & & & & &
// 3.2.添加模型对象到数组中
& &[weibosFrameArray addObject:weiboFrame]; & & & &&
} & & & & & & & & &
// 4.赋值 & & & &&
_weiboframes = _weibosframeA & &&
4.2 实现数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return self.statusFrames.
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
// 1.创建cell & &&
MJStatusCell *cell = [MJStatusCellcellWithTableView:tableView]; & & & & &
// 2.在这里已经算好了cell的高度 & &&
cell.statusFrame =self.statusFrames[indexPath.row]; & & & & &
// 3.返回cell & &&
4.3 实现代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
// 取出这行对应的frame模型 & &&
MJStatusFrame *statusFrame =self.statusFrames[indexPath.row]; & &&
return statusFrame.cellH
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 自定义tableviewcell 的文章

 

随机推荐