关于使用xib的自定义UITableViewCell中修改其中view的问题及解决

有一个如下所示的需求



//控制器

-(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



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值