UITableViewCell
2015-09-13 17:51
369 查看
一.自定义cell
1.新建继承于UITableViewCell 的子类
2.重写 初始化方法: (.m 文件)
3.根据每行显示的内容 自定义创建里面的控件 (例如:)
.h文件
.m 文件
1.新建继承于UITableViewCell 的子类
2.重写 初始化方法: (.m 文件)
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)identifier{ [self = super initWithStyle:style reuseIdentifier]; if(self){ [self createChild]; } }
3.根据每行显示的内容 自定义创建里面的控件 (例如:)
.h文件
@property (nonatomic , retain)UILabel *titleLabel; @property (nonatomic , retain)UIImageView *newsImageView; @property (nonatomic , retain)UILabel *summaryLabel; @property (nonatomic , retain)UILabel *timeLabel; #pragma mark 返回高度的方法 + (CGFloat)height; #pragma mark 给 cell 进行赋值 - (void)setCellDataWithModel:(Data *)model; #pragma mark 创建cell + (instancetype )createCellWithTableView:(UITableView *)tableView withIdentifier:(NSString *)identifier;
.m 文件
#define kScreenWidth [UIScreen mainScreen].bounds.size.width - (void)createChild{ self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, kScreenWidth - 20 * 2, 30)]; self.titleLabel.textAlignment = NSTextAlignmentCenter; self.titleLabel.font = [UIFont boldSystemFontOfSize:18]; [self.contentView addSubview:self.titleLabel]; [self.titleLabel release]; self.newsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.titleLabel.frame), CGRectGetMaxY(self.titleLabel.frame) +5 , CGRectGetWidth(self.titleLabel.frame) / 3 , 40)]; [self.contentView addSubview:self.newsImageView]; [self.newsImageView release]; self.summaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.newsImageView.frame) + 5, CGRectGetMinY(self.newsImageView.frame), CGRectGetWidth(self.titleLabel.frame) *2 / 3, CGRectGetHeight(self.newsImageView.frame))]; self.summaryLabel.alpha = 0.7; self.summaryLabel.font = [UIFont systemFontOfSize:14]; self.summaryLabel.numberOfLines = 0; [self.contentView addSubview:self.summaryLabel]; [self.summaryLabel release]; self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.summaryLabel.frame)+5, CGRectGetWidth(self.titleLabel.frame), 20)]; self.timeLabel.alpha = 0.7; self.timeLabel.textAlignment = 2; self.timeLabel.font = [UIFont systemFontOfSize:12]; [self.contentView addSubview:self.timeLabel]; [self.timeLabel release]; } #pragma mark 返回高度的方法 + (CGFloat)height; #pragma mark 给 cell 进行赋值 - (void)setCellDataWithModel:(Data *)model{ self.titleLabel.text = model.title; self.newsImageView.image = [UIImage imageNamed:@“4.jpg”]; self.summaryLabel.text = model.summary; NSTimeInterval time = [model.lastUpdateTime doubleValue]; NSDate *date = [NSDate dateWithTimeIntervalSince1970:time]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@“yyyy年MM月dd日 HH:mm”]; NSString *finalTime = [formatter StringFromDate:date]; self.timeLabel.text = finalTime; } #pragma mark 创建cell + (instancetype )createCellWithTableView:(UITableView *)tableView withIdentifier:(NSString *)identifier{ NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if(cell == nil){ cell = [[NewsCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; } return cell; }
相关文章推荐
- Windows消息队列,UI线程,窗口以及消息处理方式总结
- 使用ueditor,controller.jsp总报错
- UITextField使用的相关方法
- UI:字典的两种取值的区别
- Android build error "No rule to make target"
- 出现提示ERROR 1289 The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to have IT working
- LeetCode Repeated DNA Sequences
- uint8_t/uint16_t/uint32_t/uint64_t 等是什么数据类型
- hibernate主键生成策略 @GeneratedValue 与 @GenericGenerator
- IOS UI学习 UITableView Demo 实现类似QQ联系人收起和展开效果
- UIScreen 类
- 给UITextView添加PlaceHolder
- UI课程06 UIController及其子类,已学所有类的关系
- 百度UEditor基本使用
- UI课程05 设计模式,手势识别
- Make Project Sponsors Write Their Own Requirements
- Gradle User Guide(3)——使用命令行
- iOS8中UIBarButtonItem的setTitleTextAttributes对Disabled颜色设置无效
- Error:No suitable device found: no device found for connection "System eth0"
- The Building Blocks-Components of EA part 1- Information and Strategy