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

来源:互联网 发布:崔永元 我的抗战 知乎 编辑:程序博客网 时间:2024/05/22 15:22

有一个如下所示的需求



//控制器

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    FavoriteItemCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"FavoriteItemCell"];

   if (cell == nil)

    {

        cell = (FavoriteItemCell *)[[NSBundlemainBundle] loadNibNamed:@"FavoriteCell"owner:selfoptions:nil][0];

    }

    cell.model =_arrayOfData[indexPath.section];


    

   return cell;

}



自定义cell

@interface FavoriteItemCell :UITableViewCell


@property (weak, nonatomic) IBOutletUILabel *title_1;

@property (weak, nonatomic) IBOutletUILabel *title_2;

@property (weak, nonatomic) IBOutletUILabel *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 = [superinitWithStyle: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

{

    [selfUpdateUI];   //此处对视图内的尺寸大小修改即可解决

    

}


-(void)UpdateUI

{

    

    self.title_1.text =_model.title_1;

    self.title_2.text =_model.title_2;

    self.playNum.text = [NSStringstringWithFormat:@"%d",_model.playNum];


    UIFont *font =self.title_1.font;

    NSMutableDictionary *dict = [[NSMutableDictionaryalloc] init];

    [dict setObject:fontforKey:NSFontAttributeName];

   

    CGRect rect = [_model.title_1boundingRectWithSize:CGSizeMake(300, 50)options:NSStringDrawingUsesLineFragmentOriginattributes: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.0green:130/255.0 blue:30/255alpha:1]:[UIColorcolorWithRed:168/255.0 green:168/255.0blue:168/255.0 alpha:1]);

    

   

    NSLog(@"%@",self.title_1);

 

    

   


  

    

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

    [supersetSelected:selected animated:animated];


    // Configure the view for the selected state

}


@end



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 海岛奇兵发现求救信号怎么办 海岛奇兵被打了怎么办 小鱼翅卡喉咙了怎么办 鱼翅卡在喉咙里怎么办 斗鱼身份证被使用怎么办 做的鱼丸太腥了怎么办 做鱼丸太稀了怎么办 斗鱼手机号换了怎么办 斗鱼直播掉帧怎么办 手机一直卡顿点不动怎么办呢 斗鱼直播分值底怎么办 斗鱼6000鱼丸怎么办卡 斗鱼直播没人看怎么办 淘宝直播间没人气怎么办 挂水了还是有热度怎么办 陌陌工会不结算工资怎么办 滴滴给了差评怎么办 饿了么星级低怎么办 滴滴乘客给低星怎么办 蘑菇街自动收货前还没到怎么办 小主播人气太少别人看不到怎么办 税收分类编码不可用怎么办 斗鱼鱼翅充错了怎么办 苹果指纹摔坏了怎么办 小米5指纹坏了怎么办 苹果5s指纹失灵怎么办 学生赌博输了3万怎么办 电脑录屏没有声音怎么办 别人说你没有他美怎么办 没有你我怎么办歌词是什么意思 要是没有他我怎么办啊歌词 用喀秋莎保存的视频黑屏怎么办 电脑杀毒之后开不了机怎么办 夫妻离婚分房分车怎么办 请的护身符丢了怎么办 老车轻微烧机油怎么办 电脑下软件变卡怎么办 机械表日历偏了怎么办 子宫内膜异位痛经怎么办 凉着了坏肚子怎么办 昆虫叮咬后疼痛起水泡怎么办