关于使用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
- 关于使用xib的自定义UITableViewCell中修改其中view的问题及解决
- 使用xib自定义UITableViewCell如何修改其中的view
- 用xib自定义 可以在xib中使用的view
- iOS用xib自定义view,修改不了frame的问题
- 使用Xib自定义UITableViewCell
- 用xib自定义UITableViewCell的注意事项——重用问题
- 用xib自定义UITableViewCell的注意事项——重用问题 -
- 用xib自定义UITableViewCell的注意事项——重用问题
- 用xib自定义UITableViewCell的注意事项——重用问题
- 使用xib的UITableViewCell在编辑状态下时,排版发生变化及解决
- 使用XIB的UITableViewCell自适应,以及出现的问题进行解决
- iOS 解决xib加载UITableViewCell的宽度不适应问题
- NSLayoutConstraint 修改xib中view的高度
- 使用XIB创建自定义的View
- 关于UITableViewCell (xib) 自适应高度的问题
- XIB或者storyboard中嵌入自定义view(xib)的方法
- 如何使用xib自定义UITableViewCell xib文件的创建和添加控件
- Storyboard中使用xib定义的view
- Android Google Map V2开发环境配置图文教程
- SqlServer 并发系列之读写分离
- 黑马程序员---面向对象概述
- IdleHandler一个后娘养的handler
- Asteroids
- 关于使用xib的自定义UITableViewCell中修改其中view的问题及解决
- UVA 10878 Decode the tape
- SimpleDateFormat设置24小时制
- Hibernate多对一遍历对象的时候报java.lang.StackOverflowError错误
- 生活连接
- 初学者php
- Crontab 实现定时执行一个shell脚本(以每隔十分钟执行一次为例)
- 部分CSS缩写
- swf 嵌入 jsp 中