[TwistedFate]UITableViewCell自定义-01
2015-11-24 20:22
399 查看
自定义cell
步骤:
创建TableViewCell的子类重写初始化方法
要添加的控件添加到到cell的现实内容区域contentView上面
把系统的cell 替换成自定义cell 完成
创建MyTableViewCell类
// 自定义初始化 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self addSubViews]; } return self; } // 添加子视图 - (void)addSubViews{ self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(kMargin, kMargin, kImageWidth, kImageHeight)]; self.imageV.backgroundColor = [UIColor redColor]; [self.contentView addSubview:self.imageV]; [_imageV release]; self.nameLable = [[UILabel alloc] initWithFrame:CGRectMake(self.imageV.XWidth + kMargin, self.imageV.Y - 2.5, kScreenWidth - self.imageV.XWidth - 2 * kMargin, kImageHeight / 3 )]; self.nameLable.backgroundColor = [UIColor cyanColor]; [self.contentView addSubview:self.nameLable]; self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLable.X, self.nameLable.YHeight + 5, kScreenWidth - self.imageV.XWidth - 2 * kMargin, kImageHeight / 3 )]; self.phoneLabel.backgroundColor = [UIColor cyanColor]; [self.contentView addSubview:self.phoneLabel]; self.genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.phoneLabel.X, self.phoneLabel.YHeight + 5, kScreenWidth - self.imageV.XWidth - 2 *kMargin, kImageHeight / 3 )]; self.genderLabel.backgroundColor = [UIColor cyanColor]; [self.contentView addSubview:self.genderLabel]; }
封装以model类传值
在model类自定义的cell类中声明一个model类属性
@property (nonatomic, retain) Student *stuModel; - (void)setStuModel:(Student *)stuModel;
重写model属性的set方法
// 重写model的set方法 使赋值model的同时也给控件赋值 - (void)setStuModel:(Student *)stuModel{ if (_stuModel != stuModel) { [_stuModel release]; _stuModel = [stuModel retain]; } self.nameLable.text = stuModel.name; self.phoneLabel.text = stuModel.gender; self.genderLabel.text = stuModel.phoneNumber; self.imageV.image = [UIImage imageNamed:@"拔刀斋.jpg"]; }
返回cell
static NSString *identifier = @"MaleCell"; MaleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[[MaleTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease]; } // 以model直接赋值 Student *stu = self.dataArray[indexPath.row]; cell.stuModel = stu; return cell; } //在给model赋值时 也给cell的控件完成赋值
返回不同的cell
再创建一个自定义cell类
// 重写初始化方法 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self addSubviews]; } return self; } - (void)addSubviews{ self.nameLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 4, 40)]; self.nameLable.backgroundColor = [UIColor redColor]; [self.contentView addSubview:self.nameLable]; [_nameLable release]; } // 重写model的set方法 使赋值model的同时也给控件赋值 - (void)setStuModel:(Student *)stuModel{ if (_stuModel != stuModel) { [_stuModel release]; _stuModel = [stuModel retain]; } self.nameLable.text = stuModel.name; }
根据条件返回不同的cell
// 返回cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ Student *stu = self.dataArray[indexPath.row]; // 根据stuModel的值进行判断选用不一样的cell显示 if ([stu.gender isEqualToString:@"女"]) { // 创建女的 static NSString *identifier = @"GirlCell"; MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[[MyTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease]; } cell.stuModel = stu; return cell; }else{ // 创建男的 static NSString *identifier = @"MaleCell"; MaleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[[MaleTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease]; } cell.stuModel = stu; return cell; } // 在给model赋值时 也给cell的控件完成赋值 }
相关文章推荐
- [APUE]再读之 unix 基础知识
- setMouseTracking(true)无法跟踪鼠标事件
- HDU 2253 Longest Common Subsequence Again
- UI学习 第六章 UITableView
- UI学习 第五章 UIScrollView     UIPageControl
- UI学习 第四章 UIImage    UIImageView    手势
- easyUI..关于datagrid实现追加一行做统计功能
- UIImageView的图片轮播属性
- lightoj1048 Conquering Keokradong
- UI第十一天:UITableView ⾼级
- MIUI6&7桌面角标开源代码简介
- TextView设置android:ellipsize=marquee不滚动
- 读书笔记--Android布局和UI组件中的布局管理器
- UIScrollView 手势缩放
- UGUI 文本下划线实现
- Qt5.5中使用serialport类使得Ui界面很卡
- Handler、Message、MessageQueue、Looper调用过程源码浅析
- 10.26~11.23刷题(shui)记录
- System.Web.UI.ViewStateException: Invalid viewstate 解决方法
- 实现UITableView上View的上拉隐藏下拉出现效果(1)(category)