自定义UITableViewCell(2) 多个自定义cell
来源:互联网 发布:mac 从远程拷贝文件夹 编辑:程序博客网 时间:2024/06/09 18:00
1. 创建自定义的cell这里注意一定要添加到self.contentView上- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.labelName = [[UILabel alloc] initWithFrame:(CGRectMake(10, 0, 120, 40))]; self.labelName.backgroundColor = [UIColor orangeColor]; [self.contentView addSubview:self.labelName]; [self.labelName release]; self.labelGender = [[UILabel alloc] initWithFrame:(CGRectMake(120 + 40 + 10, 0, 120, 40))]; self.labelGender.backgroundColor = [UIColor yellowColor]; [self.contentView addSubview:self.labelGender]; [self.labelGender release]; } return self;}2. 在model的.h文件中建立枚举值// 枚举:// NSInteger枚举值的类型// CellType 枚举的名字(类型)typedef NS_ENUM(NSInteger, CellType) { // 枚举值 CellTypeOne, CellTypeTwo};@interface CellModel : NSObject@property (nonatomic, retain) NSString *name;@property (nonatomic, retain) NSString *gender;// 创建一个标签type控制显示哪种cell// type1显示第一种, type2显示第二种@property (nonatomic, assign) CellType type;@end3. 在创建数据的时候加入判断, 一定要在数据添加到数组中之前进行判断- (void)makeData { self.dataArr = [NSMutableArray arrayWithCapacity:1]; for (int i = 0; i < 10; i++) { CellModel *model = [[CellModel alloc] init]; model.name = [NSString stringWithFormat:@"姓名:%d", i]; model.gender = [NSString stringWithFormat:@"性别:%d", i]; // 给一个初始化的类型, 这里是偶数就用CellTypeOne这cell, 是奇数就用CellTypeTwo这个cell, 这里可以根据需求加入判断, 用一个界面实现展示不同的cell if (i % 2 == 0) { model.type = CellTypeOne; } else { model.type = CellTypeTwo; } [self.dataArr addObject:model]; }}4. cell的创建, 这里一定要先判断是那个cell 再创建, 避免浪费内存- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CellModel *model = self.dataArr[indexPath.row]; if (model.type == CellTypeOne) { static NSString *identifer = @"myCell"; MyTableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:identifer]; if (myCell == nil) { myCell = [[[MyTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:identifer] autorelease]; } myCell.labelName.text = model.name; myCell.labelGender.text = model.gender; return myCell; } else { static NSString *identifer2 = @"secondCell"; SecondTableViewCell *secondCell = [tableView dequeueReusableCellWithIdentifier:identifer2]; if (secondCell == nil) { secondCell = [[[SecondTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifer2] autorelease]; } secondCell.labelName.text = model.name; secondCell.labelGender.text = model.gender; return secondCell; }}5. 根据显示的不同的内容展示不同的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // 这里cellOfHeight只是设置高度的自定义函数, 在自定义cell中的.m文件中建立的 CGFloat height = [MyTableViewCell cellOfHeight]; CGFloat height2 = [SecondTableViewCell cellOfHeight]; CellModel *model = self.dataArr[indexPath.row]; if (model.type == CellTypeOne) { return height; } else { return height2; }}
0 0
- 自定义UITableViewCell(2) 多个自定义cell
- 自定义UITableViewCell重用cell
- ios中自定义cell,自定义UITableViewCell
- UITableView详解(UITableViewCell(二) 自定义cell)
- iOS UITableViewCell重用以及自定义Cell
- iOS编程------自定义UITableViewCell / cell自适应高度
- 自定义带有cell间距的UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell
- 自定义UITableViewCell。。。。
- 自定义UITableViewCell
- 自定义UITableViewCell
- iOS到Android到底有多远
- HDU 1205 吃糖果
- 面向对象(this关键字)-第11天
- Service
- 学习OpenGL好地址。
- 自定义UITableViewCell(2) 多个自定义cell
- HBase1.0以上版本的API改变
- Linux学习笔记---目录处理命令---删除文件或目录
- 关于使用openfiler作为共享存储来安装rac时的问题
- Java Servlet完全教程
- Chapter 16 notification iOS 8 -Swift Programming cookBook 读书笔记
- CELL⾃适应⾼度
- JMeter--web性能测试使用笔记
- [网狐]游戏添加方法