关于使用xib的自定义UITableViewCell中修改其中view的问题及解决
2014-07-15 09:57
411 查看
有一个如下所示的需求
//控制器
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
FavoriteItemCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"FavoriteItemCell"];
if (cell == nil)
{
cell = (FavoriteItemCell *)[[NSBundle
mainBundle] loadNibNamed:@"FavoriteCell"
owner:self
options:nil][0];
}
cell.model =
_arrayOfData[indexPath.section];
return cell;
}
自定义cell
@interface FavoriteItemCell :
UITableViewCell
@property (weak,
nonatomic) IBOutlet
UILabel *title_1;
@property (weak,
nonatomic) IBOutlet
UILabel *title_2;
@property (weak,
nonatomic) IBOutlet
UILabel *playNum;
@property(nonatomic,retain)FavoriteItem * model;
@end
//
// FavoriteItemCell.m
// LearningEnglish
//
// Created by w on 14-7-14.
// Copyright (c) 2014年 xxx.com. All rights reserved.
//
#import "FavoriteItemCell.h"
@implementation FavoriteItemCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier
{
self = [super
initWithStyle:style
reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return
self;
}
- (void)awakeFromNib
{
// Initialization code
}
-(void)setModel:(FavoriteItem *)model
{
_model = model;
//[self UpdateUI] 尝试过在此次对里面的子视图进行修改,但初次显示的内容尺寸无法修改,当这个cell被重用时可以达到修改效果,也深度在此时通过定时器延时进行修改,可解决,可有时会有延时的效果,不完美...
}
//重载如下方法也无法达到修改效果
//-(void)layoutSubviews
//{
// [self layoutSubviews];
// [self UpdateUI];
//}
//此处调用UpdateUI完美解决
-(void)drawRect:(CGRect)rect
{
[self
UpdateUI];
//此处对视图内的尺寸大小修改即可解决
}
-(void)UpdateUI
{
self.title_1.text =
_model.title_1;
self.title_2.text =
_model.title_2;
self.playNum.text = [NSString
stringWithFormat:@"%d次",_model.playNum];
UIFont *font =
self.title_1.font;
NSMutableDictionary *dict = [[NSMutableDictionary
alloc] init];
[dict setObject:font
forKey:NSFontAttributeName];
CGRect rect = [_model.title_1
boundingRectWithSize:CGSizeMake(300, 50)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:dict context:nil];
self.title_1.frame =
CGRectMake(self.title_1.frame.origin.x,
self.title_1.frame.origin.y, rect.size.width,
self.title_1.frame.size.height);
CGRect frame =
self.playNum.frame;
frame.origin.x =
self.title_1.frame.origin.x + rect.size.width
+ 5;
self.playNum.frame = frame;
self.playNum.backgroundColor = (_model.playNum
!= 0?[UIColor colorWithRed:254/255.0
green:130/255.0 blue:30/255
alpha:1]:[UIColor
colorWithRed:168/255.0 green:168/255.0
blue:168/255.0 alpha:1]);
NSLog(@"%@",self.title_1);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super
setSelected:selected
animated:animated];
// Configure the view for the selected state
}
@end
//控制器
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
FavoriteItemCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"FavoriteItemCell"];
if (cell == nil)
{
cell = (FavoriteItemCell *)[[NSBundle
mainBundle] loadNibNamed:@"FavoriteCell"
owner:self
options:nil][0];
}
cell.model =
_arrayOfData[indexPath.section];
return cell;
}
自定义cell
@interface FavoriteItemCell :
UITableViewCell
@property (weak,
nonatomic) IBOutlet
UILabel *title_1;
@property (weak,
nonatomic) IBOutlet
UILabel *title_2;
@property (weak,
nonatomic) IBOutlet
UILabel *playNum;
@property(nonatomic,retain)FavoriteItem * model;
@end
//
// FavoriteItemCell.m
// LearningEnglish
//
// Created by w on 14-7-14.
// Copyright (c) 2014年 xxx.com. All rights reserved.
//
#import "FavoriteItemCell.h"
@implementation FavoriteItemCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier
{
self = [super
initWithStyle:style
reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return
self;
}
- (void)awakeFromNib
{
// Initialization code
}
-(void)setModel:(FavoriteItem *)model
{
_model = model;
//[self UpdateUI] 尝试过在此次对里面的子视图进行修改,但初次显示的内容尺寸无法修改,当这个cell被重用时可以达到修改效果,也深度在此时通过定时器延时进行修改,可解决,可有时会有延时的效果,不完美...
}
//重载如下方法也无法达到修改效果
//-(void)layoutSubviews
//{
// [self layoutSubviews];
// [self UpdateUI];
//}
//此处调用UpdateUI完美解决
-(void)drawRect:(CGRect)rect
{
[self
UpdateUI];
//此处对视图内的尺寸大小修改即可解决
}
-(void)UpdateUI
{
self.title_1.text =
_model.title_1;
self.title_2.text =
_model.title_2;
self.playNum.text = [NSString
stringWithFormat:@"%d次",_model.playNum];
UIFont *font =
self.title_1.font;
NSMutableDictionary *dict = [[NSMutableDictionary
alloc] init];
[dict setObject:font
forKey:NSFontAttributeName];
CGRect rect = [_model.title_1
boundingRectWithSize:CGSizeMake(300, 50)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:dict context:nil];
self.title_1.frame =
CGRectMake(self.title_1.frame.origin.x,
self.title_1.frame.origin.y, rect.size.width,
self.title_1.frame.size.height);
CGRect frame =
self.playNum.frame;
frame.origin.x =
self.title_1.frame.origin.x + rect.size.width
+ 5;
self.playNum.frame = frame;
self.playNum.backgroundColor = (_model.playNum
!= 0?[UIColor colorWithRed:254/255.0
green:130/255.0 blue:30/255
alpha:1]:[UIColor
colorWithRed:168/255.0 green:168/255.0
blue:168/255.0 alpha:1]);
NSLog(@"%@",self.title_1);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super
setSelected:selected
animated:animated];
// Configure the view for the selected state
}
@end
相关文章推荐
- iOS 使用xib定义一个View,修改frame无效问题解决
- 自定义ViewFlow使用,ScrollView中嵌套ViewFlow滑动问题解决(---My 20150407)
- 关于自定义View时,画图形和图片时抗锯齿的使用的问题
- 解决了一下关于list中使用viewHolder加载错乱的问题
- 关于VC多字节字符集和unicode宽字符集的使用和问题,只有明白了其中的原理,才能灵活解决问题
- iOS开发-自定义TabBar使用popToRootViewControllerAnimated重叠问题解决
- 使用XIB的UITableViewCell自适应,以及出现的问题进行解决
- 关于TP5视图分离到根目录的解决方法 原有: 为了方便前端开发,我想把视图(view)部分分离出来专门给前端进行修改 过程:在进行视图设置分离的过程遇到了问题 参考了官方文档http://www
- ios --xib自定义,解决在导航栏不透明的情况下,自定义xib view高度被压缩64的问题
- 关于自定义View显示gif图片的问题解决
- 使用XIB的UITableViewCell自适应,以及出现的问题进行解决
- 关于自定义View时,画图形和图片时抗锯齿的使用的问题
- 关于PreferenceActivity的使用和一些问题的解决(自定义Title和取值)
- 关于上位机与arm使用自定义协议通过串口传输文件时遇到的疑难问题的解决
- 关于VC多字节字符集和unicode宽字符集的使用和问题,只有明白了其中的原理,才能灵活解决问题
- 安卓关于自定义View只能接收ACTION_DOWN事件问题的解决
- 关于自定义View时,画图形和图片时抗锯齿的使用的问题
- 关于使用spring框架来实现email发送其中遇到的问题已解决
- 关于华为手机自定义view中NavigationBar显示与隐藏造成高度问题的解决办法
- iOS用xib自定义view,修改不了frame的问题