您的位置:首页 > 产品设计 > UI/UE

关于使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐