请教如何在ios tableviewcelll中显示html文字

iOS Cell嵌套UIWebView(内附UIWebView详解)
我的图书馆
iOS Cell嵌套UIWebView(内附UIWebView详解)
背景:最近做的项目中有这样一个需求,一个话题详情界面内部分内容为html标签,其他为普通内容,然后html标签是嵌套在Cell中的,一开始用的是UILabel加载html标签,结果发现对于图片标签没有更好的适应屏幕,果断换成UIWebView,使用WebView加载计算高度的时候是有些注意点的,所以在此记录一下,并总结一下相关知识,以备后续查阅。
一、cell嵌套webView具体代码及讲解
1、自定义Cell并初始化数据
if (indexPath.section == 0) {
LSTopicDetailMainCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithUTF8String:object_getClassName([LSTopicDetailMainCell class])] forIndexPath:indexPath];
cell.viewModel = self.viewModel.topMainCellViewM
使用viewModel装配自定义Cell,注意其identifier已经注册,如下
[_mainTableView registerClass:[LSTopicDetailMainCell class] forCellReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([LSTopicDetailMainCell class])]];
2、返回高度
比较麻烦的是高度的计算,因为我对于Cell自动布局高度的计算是用的 UITableView FDTemplateLayoutCell 这个第三方,核心是提前计算高度及缓存,对UILabel加载Html标签来说很OK,但是对于webView来讲就有些问题,因为他的高度需要在加载完的回调中去获取并刷新,所以需要手动计算。
if (indexPath.section == 0) {
return self.viewModel.topMainCellViewModel.cellH
3、自定义Cell内相关代码
加载Html标签
[self.contentWebView loadHTMLString:viewModel.content baseURL:nil];
UIWebView回调处理(核心代码)
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// 获取内容高度
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@'document.documentElement.scrollHeight'] intValue];
// 防止死循环
if (height != _viewModel.htmlHeight) {
_viewModel.htmlHeight =
if (_viewModel.htmlHeight & 0) {
// 更新布局
CGFloat paddingEdge = 10;
WS(weakSelf)
[self.contentWebView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(paddingEdge);
make.right.equalTo(-paddingEdge);
make.top.equalTo(weakSelf.headerImageView.mas_bottom).offset(paddingEdge);
make.bottom.equalTo(-paddingEdge);
// 刷新cell高度
_viewModel.cellHeight = _viewModel.otherHeight _viewModel.htmlH
[_viewModel.refreshSubject sendNext:nil];
上面的代码注释已经很清楚了,需要解释的是防止死循环的意思是你刷新cell的代码在回调里,当你刷新的时候,他也会走回调,不判断处理的话会造成死循环。
4、刷新Cell
[self.viewModel.topMainCellViewModel.refreshSubject subscribeNext:^(id x) {
@strongify(self);
[self.mainTableView reloadSection:0 withRowAnimation:UITableViewRowAnimationNone];
二、Cell嵌套UILabel加载Html标签处理
假如你只是需要处理文字相关的Html标签的话,使用Label加载是最好的选择
1、高度处理(核心代码)
if (indexPath.section == 0) {
return [tableView fd_heightForCellWithIdentifier:[NSString stringWithUTF8String:object_getClassName([LSTopicDetailMainCell class])] cacheByIndexPath:indexPath configuration:^(LSTopicDetailMainCell *cell) {
@strongify(self);
cell.viewModel = self.viewModel.topMainCellViewM
2、Masonry布局
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(paddingEdge);
make.right.equalTo(-paddingEdge);
make.top.equalTo(weakSelf.headerImageView.mas_bottom).offset(paddingEdge);
make.bottom.equalTo(-paddingEdge);
3、Label加载Html
NSAttributedString *content = [[NSAttributedString alloc] initWithData:[viewModel.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
self.contentLabel.attributedText =
三、UIWebView详解
1、三种加载方式
加载URL内容
NSURL *url = [NSURL URLWithString:@'/users/1a9cd48c3cf0/latest_articles'];
[self.webview loadRequest:[NSURLRequest requestWithURL:url]];
加载本地的HTML文件
NSString *htmlPath = [[[NSBundle mainBundle] bundlePath]
stringByAppendingPathComponent:@'wanglongshuai.html'];
[self.webview loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:htmlPath]]];
加载html字符串
NSString *htmlPath = [[[NSBundle mainBundle] bundlePath]
stringByAppendingPathComponent:@'wanglongshuai.html'];
NSString *htmlString = [NSString stringWithContentsOfFile: htmlPath
encoding:NSUTF8StringEncoding
error:NULL];
[self.webview loadHTMLString:htmlString baseURL:[NSURL
fileURLWithPath:htmlPath]];
2、UIWebViewDelegate代理方法
//准备加载内容: 通过返回值来进行是否加载的设置
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationT
//开始加载
- (void)webViewDidStartLoad:(UIWebView *)webV
//加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webV
//加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)
3、常用属性及API
基本属性及API
**webView的代理**
@property (nullable, nonatomic, assign) id &UIWebViewDelegate&
**内置的scrollView**
@property (nonatomic, readonly, strong) UIScrollView *scrollView NS_AVAILABLE_IOS(5_0);
**URL请求**
@property (nullable, nonatomic, readonly, strong) NSURLRequest *
**是否缩放到适合屏幕大小**
@property (nonatomic) BOOL scalesPageToF
**执行javaScript操作**
- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)
加载相关属性及API
- (void) //重新加载数据
- (void)stopL //停止加载数据
@property (nonatomic, readonly, getter=isLoading) BOOL //是否正在加载
- (void)goB //返回上一级
- (void)goF //跳转下一级
@property (nonatomic, readonly, getter=canGoBack) BOOL canGoB //能否返回上一级
@property (nonatomic, readonly, getter=canGoForward) BOOL canGoF //能否跳转下一级
视频相关属性
//YES,自动检测网页上的电话号码,单击可以拨打
@property (nonatomic) BOOL detectsPhoneNumbers NS_DEPRECATED_IOS(2_0, 3_0);
//设置某些数据变为链接形式,这个枚举可以设置如电话号,地址,邮箱等转化为链接
@property (nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0);
//设置是否使用内联播放器播放视频
@property (nonatomic) BOOL allowsInlineMediaPlayback NS_AVAILABLE_IOS(4_0); // iPhone Safari defaults to NO. iPad Safari defaults to YES
//设置视频是否自动播放
@property (nonatomic) BOOL mediaPlaybackRequiresUserAction NS_AVAILABLE_IOS(4_0); // iPhone and iPad Safari both default to YES
//设置音频播放是否支持ari play功能
@property (nonatomic) BOOL mediaPlaybackAllowsAirPlay NS_AVAILABLE_IOS(5_0); // iPhone and iPad Safari both default to YES
//设置是否将数据加载如内存后渲染界面
@property (nonatomic) BOOL suppressesIncrementalRendering NS_AVAILABLE_IOS(6_0); // iPhone and iPad Safari both default to NO
//设置用户交互模式
@property (nonatomic) BOOL keyboardDisplayRequiresUserAction NS_AVAILABLE_IOS(6_0); // default is YES
著作权归作者所有
TA的最新馆藏
喜欢该文的人也喜欢匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。如何在Tableview中使用UiTextFiled
关于UITextField开始编辑时页面上滚的效果
重写UITextFieldDelegate的方法
-(void)textFieldDidBeginEditing:(UITextField *)textField
if(textField==userNameTextField) //或者根据tag if(textField.tag)
[rootScrollView setContentOffset:CGPointMake(0, 100)
animated:YES];
-(void)textFieldDidEndEditing:(UITextField *)textField
[rootScrollView setContentOffset:CGPointMake(0, 0)
animated:YES];
或者在初始化UITextField对象实例时添加事件
[checkCodeTextField addTarget:self action:@selector(upLayout)
forControlEvents:UIControlEventEditingDidBegin];
[checkCodeTextField addTarget:self action:@selector(downLayout)
forControlEvents:UIControlEventEditingDidEnd];
——————————————&
关于文本框的输入
在一个文本框输入结束返回的时候,释放自身的响应级别resignFirstResponder,让下一个文本框成为焦点,即变成第一响应控件becomeFirstResponder,具体为:重写UITextFieldDelegate方法。下面的写法前提是在定义文本框时将其tag设为相连的几个整值。
-(BOOL)textFieldShouldReturn:(UITextField *)textField
[textField resignFirstResponder];
if([self.view viewWithTag:textField.tag+1])
[[self.view viewWithTag:textField.tag+1]
becomeFirstResponder];
return YES;
UITableViewCell的contentView中的UITextField的值获取有几种方法,本人简单总结一下。
&获取UITextField所以Cell的NSIndexPath,知道了NSIndexPath就知道了这个UITextField是干什么的了。
-&(BOOL)textField:(UITextField&*)textField&shouldChangeCharactersInRange:(NSRange)range&replacementString:(NSString&*)string&&
//get&cell&&
UITableViewCell&*cell&=&[UITableViewCell&][[textField&superview]&superview];&&
NSIndexPath&*indexPath&=&[tableView&indexPathForCell:cell];&&
-&(void)textFieldDidEndEditing:(UITextField&*)textField&&
&&&&//get&cell&&
&&&&UITableViewCell&*cell&&=&(UITableViewCell&*)[[textField&superview]&superview];&&
&&&&NSIndexPath&*indexPath&=&[tableView&indexPathForCell:cell];&&
中得知道UITextField中text是哪一个数据结构的值,前一个是实时的,后一个是失去焦点时一次性的。
2。第二种方法与上面第一个有点类似也是实时的,来自:
-&(UITableViewCell&*)tableView:(UITableView&*)tableView&cellForRowAtIndexPath:(NSIndexPath&*)indexPath&&
&&&&NSInteger&row&=&[indexPath&row];&&
&&&&static&NSString&&*CellIdentifier&=&@"CellIdentifier";&&
&&&&UITableViewCell&*cell&=&[tableView&dequeueReusableCellWithIdentifier:CellIdentifier];&&
&&&&if&(cell&==&nil)&{&&
&&&&&&&&cell&=&[[[UITableViewCell&alloc]&initWithStyle:UITableViewCellStyleDefault&reuseIdentifier:CellIdentifier]&autorelease];&&
&&&&&&&&cell.selectionStyle&=&UITableViewCellSelectionStyleNone;&&
&&&&cell.textLabel.text&=&[_passwordArray&objectAtIndex:row];&&
&&&&CGRect&textFieldRect&=&CGRectMake(0.0,&0.0f,&215.0f,&31.0f);&&
&&&&UITextField&*theTextField&=&[[UITextField&alloc]&initWithFrame:textFieldRect];&&
&&&&theTextField.contentVerticalAlignment&=&UIControlContentVerticalAlignmentCenter;&&
&&&&theTextField.returnKeyType&=&UIReturnKeyDone;&&
&&&&theTextField.secureTextEntry&=&YES;&&
&&&&theTextField.clearButtonMode&=&YES;&&
&&&&theTextField.tag&=&row;&&
&&&&theTextField.delegate&=&self;&&
//此方法为关键方法&&
&&&&[theTextField&addTarget:self&action:@selector(textFieldWithText:)&forControlEvents:UIControlEventEditingChanged];&&
&&&&switch&(row)&{&&
&&&&&&&&case&0:&&
&&&&&&&&&&&&theTextField.placeholder&=&@"请输入旧密码";&&
&&&&&&&&&&&&break;&&
&&&&&&&&case&1:&&
&&&&&&&&&&&&theTextField.placeholder&=&@"请输入新密码";&&
&&&&&&&&&&&&break;&&
&&&&&&&&case&2:&&
&&&&&&&&&&&&theTextField.placeholder&=&@"请再次输入新密码";&&
&&&&&&&&&&&&break;&&
&&&&&&&&default:&&
&&&&&&&&&&&&break;&&
&&&&cell.accessoryView&=&theTextField;&&&
&&&&[theTextField&release];&&
&&&&return&cell;&&
-&(void)textFieldWithText:(UITextField&*)textField&&
&&&&switch&(textField.tag)&{&&
&&&&&&&&case&0:&&
&&&&&&&&&&&&self.theOldPassword&=&textField.text;&&
&&&&&&&&&&&&break;&&
&&&&&&&&case&1:&&
&&&&&&&&&&&&self.theNewPassword&=&textField.text;&&
&&&&&&&&&&&&break;&&
&&&&&&&&case&2:&&
&&&&&&&&&&&&self.theTwiceNewPassword&=&textField.text;&&
&&&&&&&&&&&&break;&&
&&&&&&&&default:&&
&&&&&&&&&&&&break;&&
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Pages: 1/3
主题 : 请教如何在tableviewcell中显示html文字(带img标签的html)
级别: 侠客
UID: 447052
可可豆: 489 CB
威望: 413 点
在线时间: 243(时)
发自: Web Page
来源于&&分类
请教如何在tableviewcell中显示html文字(带img标签的html)&&&
关于这个,我真的是没有做过,本来想用webView,可是webview不知道是哪一个cell的webview,一直没能实现
级别: 精灵王
UID: 54474
可可豆: 3655 CB
威望: 3652 点
在线时间: 1041(时)
发自: Web Page
说具体点?
级别: 新手上路
可可豆: 2 CB
威望: 2 点
在线时间: 146(时)
发自: Web Page
我知道的有两种方式:1. 使用UITextView,然后使用NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];对textView的attributedText赋值。2. 使用DTCoreText第三方库。
级别: 侠客
UID: 447052
可可豆: 489 CB
威望: 413 点
在线时间: 243(时)
发自: Web Page
回 1楼(coco89718) 的帖子
首先,后台拿到的数据是html数据(带图片的&img src&),要在页面上显示出来,一个页面比如有5个cell,我就不知道怎么加载这些数据了,我就在cell中放入webview,可是cell的高度是根据内容来变化高度的.webview加载完才能算出高度,我就不知道怎么去设置cell的高度了.我的想法是webview加载完重新刷新tableview,高度用字典来设置.可是我不知道是哪个webview对应的那个cell,也不知道是哪一个webview加载完成了,我应该怎么去做
级别: 侠客
UID: 447052
可可豆: 489 CB
威望: 413 点
在线时间: 243(时)
发自: Web Page
回 2楼(leoking870) 的帖子
textview能显示网络的图片?
级别: 新手上路
可可豆: 174 CB
威望: 174 点
在线时间: 328(时)
发自: Web Page
请问楼主解决了吗?我也需要实现这样的功能,服务器返回的是html代码
级别: 侠客
UID: 447052
可可豆: 489 CB
威望: 413 点
在线时间: 243(时)
发自: Web Page
回 5楼() 的帖子
没有解决呢
级别: 新手上路
可可豆: 174 CB
威望: 174 点
在线时间: 328(时)
发自: Web Page
回 6楼(修罗的爱) 的帖子
蛋疼,我遇到的问题跟你一样
级别: 侠客
UID: 447052
可可豆: 489 CB
威望: 413 点
在线时间: 243(时)
发自: Web Page
回 7楼() 的帖子
解决了说一声哈
级别: 新手上路
UID: 524077
可可豆: 84 CB
威望: 78 点
在线时间: 175(时)
发自: Web Page
Pages: 1/3
关注本帖(如果有新回复会站内信通知您)
苹果公司现任CEO是谁?2字 正确答案:库克
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版& & & 之前已经实现过通过简单的XIB文件来自定义我们的tableViewCell,包括每一步的步骤和代码:/daomul/p/4355999.html
& & & 现在我们采取另外一种方式,通过纯编写代码来实现自定义我们的tableview,那么什么时候使用XIB,是这样的,当我们的cell的高度是固定的时候,我们可以采用XIB文件,如果我们的cell高度是不固定的,那么就需要使用代码编写了
话不多说,上一个没有放图,这个先看我们大概做出来的效果如下:
三、 实现步骤
四、代码清单
ViewController.m:
ViewController.m
codeDefinedForTableviewcell
Created by bos on 15-3-22.
Copyright (c) 2015年 axiba. All rights reserved.
9 #import "ViewController.h"
10 #import "weiboCell.h"
11 #import "Weibo.h"
12 #import "WeiboFrame.h"
14 @interface ViewController ()
//全局变量,对应着从文件中取得的数据
//NSMutableArray *_
NSMutableArray *_weiboF
23 @implementation ViewController
25 - (void)viewDidLoad {
[super viewDidLoad];
//从plist文件中加载数据数组
NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil]];
//遍历添加到我们的微博模型中
_weiboFrames = [NSMutableArray array];
for (NSDictionary *dict in arr) {
WeiboFrame *wframe = [[WeiboFrame alloc]init];
wframe.weibo
= [Weibo weiboWithDict:dict];
[_weiboFrames addObject:wframe];
/*_weibos = [NSMutableArray array];
for (NSDictionary *key in arr) {
//每一个可变数组中存放每条模型(多少条就是对应着多少行 )
[_weibos addObject:[Weibo weiboWithDict:key]];
47 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
52 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return _weiboFrames.
57 -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//1、去缓存池取出cell
weiboCell *cell = [tableView dequeueReusableCellWithIdentifier:[weiboCell getID]];
//2、如果不存在缓存,则创建一个
if (cell ==nil) {
cell = [[weiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[weiboCell getID]];
//3、传递模型数据(相当于setWeibo,没有传递则)
//WeiboFrame *wframe = [[WeiboFrame alloc] init];
//wframe.weibo = _weiboFrames[indexPath.row];
cell.weiboframe = _weiboFrames[indexPath.row];
74 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//WeiboFrame *wframe = [[WeiboFrame alloc] init];
//wframe.weibo = _weibofr[indexPath.row];
//return wframe.cellH
return [_weiboFrames[indexPath.row] cellHeight];
WeiboCell.h
weiboCell.h
codeDefinedForTableviewcell
Created by bos on 15-3-22.
Copyright (c) 2015年 axiba. All rights reserved.
9 #import &UIKit/UIKit.h&
10 @class WeiboF
12 @interface weiboCell : UITableViewCell
14 @property (nonatomic,strong) UILabel *
15 @property (nonatomic,strong) UILabel *
16 @property (nonatomic ,strong) UILabel *
17 @property (nonatomic,strong) UIImageView *vipI
18 @property (nonatomic,strong) UIImageView *picI
19 @property (nonatomic,strong) UILabel *contentL
20 @property (nonatomic,strong) UILabel *
22 @property (nonatomic,strong) WeiboFrame *
24 +(NSString *)getID;
WeiboCell.m
weiboCell.m 模型显示到我们的cell里面
codeDefinedForTableviewcell
Created by bos on 15-3-22.
Copyright (c) 2015年 axiba. All rights reserved.
9 #import "weiboCell.h"
10 #import "Weibo.h"
11 #import "WeiboFrame.h"
13 #define kCellBorder 10
14 #define kIconWH 40
16 @interface weiboCell()
UIImageView *_
UILabel *_
UILabel *_
UIImageView *_
UIImageView *_
UILabel *_
UILabel *_
29 @implementation weiboCell
31 -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//不清楚高度则先添加基本得内部所有子控件
[self setData];
43 #pragma
设置微博数据以及高度frame
44 -(void)setWeiboframe:(WeiboFrame *)weiboframe
//1.将weibo负值给我们的weibo模型
_weiboframe =
//2.设置微博数据
[self settingWeiboData];
//3.设置子控件的frame
[self settingWeiboFrame];
55 #pragma
设置微博数据
56 -(void)settingWeiboData
Weibo *weibo = _weiboframe.
_icon.image = [UIImage imageNamed:weibo.icon];
_name.text = weibo.
_content.text = weibo.
_content.numberOfLines = 0;
_from.text = weibo.
_vip.hidden = !weibo.
_time.text = weibo.
if (weibo.pic) {
_pic.hidden = NO;
_pic.image = [UIImage imageNamed:weibo.pic];
_pic.hidden = YES;
83 #pragma
设置微博高度frame
84 -(void) settingWeiboFrame
_icon.frame = _weiboframe.iconF
_name.frame = _weiboframe.nameF
//3VIP图标
_vip.frame = _weiboframe.vipF
_time.frame = _weiboframe.timeF
_from.frame = _weiboframe.fromF
_content.frame = _weiboframe.contentF
if (_weiboframe.weibo.pic) {
_pic.frame = _weiboframe.picF
115 #pragma
添加基本的内部控件
116 -(void)setData
= [[UIImageView alloc] init];
[self.contentView addSubview:_icon];
//2、会员姓名
_name = [[UILabel alloc] init];
[self.contentView addSubview: _name];
_time = [[UILabel alloc] init];
_time.font = [UIFont systemFontOfSize:12];
[self.contentView addSubview:_time];
_content= [[UILabel alloc] init];
[self.contentView addSubview:_content];
//5、VIP图标
_vip = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vip.png"]];
[self.contentView addSubview:_vip];
//6、微博配图
_pic= [[UIImageView alloc] init];
[self.contentView addSubview:_pic];
_from = [[UILabel alloc] init];
_from.font = _time.
[self.contentView addSubview:_from];
151 +(NSString *)getID
return @"cell";
codeDefinedForTableviewcell
Created by bos on 15-3-22.
Copyright (c) 2015年 axiba. All rights reserved.
9 #import &Foundation/Foundation.h&
11 @interface Weibo : NSObject
13 @property (nonatomic,copy) NSString *
14 @property (nonatomic,copy) NSString *
15 @property (nonatomic,copy) NSString *
16 @property (nonatomic, copy) NSString *
17 @property (nonatomic,copy) NSString *
18 @property (nonatomic,copy) NSString *
19 @property (nonatomic,assign) BOOL
21 -(id) initWithDict:(NSDictionary *)
22 +(id) weiboWithDict:(NSDictionary *)
Weibo.m 模型
codeDefinedForTableviewcell
Created by bos on 15-3-22.
Copyright (c) 2015年 axiba. All rights reserved.
9 #import "Weibo.h"
11 @implementation Weibo
13 -(id) initWithDict:(NSDictionary *)dict
if (self == [super init]) {
self.name =dict[@"name"];
self.icon = dict[@"icon"];
self.time = dict[@"time"];
self.content = dict[@"desc"];
self.from = dict[@"from"];
self.pic = dict[@"pic"];
self.vip = [dict[@"vip"] boolValue];
27 //必须实现的类方法,否则类无法alloc 自己(self)
28 +(id) weiboWithDict:(NSDictionary *)dict
return [[self alloc] initWithDict:dict];
WeiboFrame.h
WeiboFrame.h
codeDefinedForTableviewcell
Created by bos on 15-4-8.
Copyright (c) 2015年 axiba. All rights reserved.
7 // 用来存放某一个cell内部所有的子控件 的frame
9 #import &Foundation/Foundation.h&
10 #import &UIKit/UIKit.h&
12 @class W
13 @interface WeiboFrame : NSObject
16 // 头像 的frame
17 @property (nonatomic,assign,readonly) CGRect iconF
18 // 是不是大V 的frame
19 @property (nonatomic,assign,readonly) CGRect vipF
20 // 名字 的frame
21 @property (nonatomic,assign,readonly) CGRect nameF
22 // 发表时间 的frame
23 @property (nonatomic,assign,readonly) CGRect timeF
24 // 正文内容 的frame
25 @property (nonatomic,assign,readonly) CGRect contentF
26 // 大图片 的frame
27 @property (nonatomic,assign,readonly) CGRect picF
28 // 来自客户端 的frame
29 @property (nonatomic,assign,readonly) CGRect fromF
31 @property (nonatomic,assign,readonly) CGFloat cellH
32 @property (nonatomic,strong) Weibo *
WeiboFram.m
WeiboFrame.m
codeDefinedForTableviewcell
Created by bos on 15-4-8.
Copyright (c) 2015年 axiba. All rights reserved.
7 // yonglai
10 #define kCellBorder 10
11 #define kIconWH 40
13 #import "WeiboFrame.h"
14 #import "Weibo.h"
16 @implementation WeiboFrame
18 -(void) setWeibo:(Weibo *)weibo
CGFloat iconX = kCellB
CGFloat iconY = kCellB
_iconFrame = CGRectMake(iconX, iconY, kIconWH, kIconWH);
CGFloat nameX = CGRectGetMaxX(_iconFrame) + kCellB
CGFloat nameY = iconY;
//宽高取决于文字宽度
CGSize nameSize = [self getLabelHeighDynamic:_weibo.name];
_nameFrame = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
//3VIP图标
CGFloat vipX = CGRectGetMaxX(_nameFrame) + kCellB
CGFloat vipY = nameY;
_vipFrame = CGRectMake(vipX, vipY, 20, 20);
CGFloat timeX = nameX;
CGFloat timeY = CGRectGetMaxY(_nameFrame) + kCellB
CGSize timeSize =
[self getLabelHeighDynamic:_weibo.time];
_timeFrame = CGRectMake(timeX, timeY, timeSize.width, timeSize.height);
CGFloat fromX = CGRectGetMaxX(_timeFrame) +kCellB
CGFloat fromY = timeY;
CGSize fromSize = [self getLabelHeighDynamic: _weibo.from];
_fromFrame = CGRectMake(fromX, fromY, fromSize.width, fromSize.height);
CGFloat contentX = iconX;
CGFloat contentY = MAX(CGRectGetMaxY(_iconFrame), CGRectGetMaxY(_timeFrame));
CGRect contentSize = [self getContentFrameDynamic: _weibo.content];
_contentFrame = CGRectMake(contentX, contentY, contentSize.size.width, contentSize.size.height);
if (_weibo.pic.length & 0) {
CGFloat picX = contentX;
CGFloat picY = CGRectGetMaxY(_contentFrame) +kCellB
_picFrame = CGRectMake(picX, picY, 80, 80);
_cellHeight = CGRectGetMaxY(_picFrame) + kCellB
_cellHeight = CGRectGetMaxY(_contentFrame) +kCellB
75 #pragma 根据文字长度动态确定label的高度
76 -(CGSize)getLabelHeighDynamic:(NSString *)word
UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:18.0f];
CGSize size = [word sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
83 #pragma 根据正文内容多少,动态确定正文content的frame
85 -(CGRect)getContentFrameDynamic:(NSString *)word
CGFloat contentW = 320 - 2 *kCellB
// label的字体 HelveticaNeue
UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:18.0f];
//_content.font =
//_content.lineBreakMode = NSLineBreakByWordW
// iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法
CGRect tmpRect = [word boundingRectWithSize:CGSizeMake(contentW, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil];
return tmpR
&五、遇到的问题总结
&1、number函数写错, 记得是tableview先写,否则导致无法执行cellforRowAtIndexpath无法执行
&2、忘记传递模型数据,导致set方法没有执行
&3、IOS7中用以下方法
&& & - (CGSize)sizeWithAttributes:(NSDictionary *)
&& & 替代过时的iOS6中的- (CGSize)sizeWithFont:(UIFont *)font 方法
&4、如果头文件中遇到类似于&编绎显示Unknown type name &CGFloat&& 错误解决方法&
& &直接加头文件:
& & &#import &UIKit/UIKit.h&或者将Compile Sources As 改为 Objective-C++
&5、在ViewDidLoad使用以下方法在取缓存池的值的时候,可以不用判断是否为空,会自动取缓冲取值,适用于6.0+
&[self.tableView registerClass:[Mycell class] forCellReuseIdenifier:@"cellID"];
六、源代码下载地址:
& & /s/1kTqsTpH
阅读(...) 评论()}

我要回帖

更多关于 tableviewcell的样式 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信