- 直接添加数据,需要多个
if,else...
的判断,特别的繁琐.
-
使用数组+字典来存放数据,拼写错误时,很难发现.
- 使用模型来存放数据,无繁琐的判断,没有拼写错误的情况.
步骤如下:-
将每组数据,都用一个组模型来存放.
-
组模型里面有个数组, 专门存放产品模型 (也叫模型嵌套)
-
组模型里面有个数组, 专门存放产品模型 (也叫模型嵌套)
将多组数据形成的多个组模型,存放在一个大的数组里.
@implementation Car + (instancetype)carWithIcon:(NSString *)icon name:(NSString *)name{ Car *car = [[self alloc]init]; car.icon = icon; car.name = name; return car; } @end
@property (nonatomic ,strong) NSArray *carData; -(NSArray *)carData{ if(_carData == nil){ CarGroup *dgGroup = [[CarGroup alloc]init]; dgGroup.headInfo = @"德国系列"; dgGroup.endInfo = @"你瞅啥..."; dgGroup.cars = @[ [Car carWithIcon:@"m_2_100"name:@"奔驰"], [Car carWithIcon:@"m_3_100" name:@"宝马"] ]; ...... self.carData = @[dgGroup]; //多个组模型存放在一个大的数组里 } return _carData; }
- 获取对应的数据.
//有多少组,默认为1组 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.carData.count; } //每组有多少行数据. (Section -当前组) - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { CarGroup *group = self.carData[section]; return group.cars.count; }
//每行显示的内容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cellTemp = [[UITableViewCell alloc]init]; CarGroup * group = self.carData[indexPath.section]; // 获取对应组. Car *carTemp = group.cars[indexPath.row]; //当前组里面的对应行 cellTemp.textLabel.text = carTemp.name; cellTemp.imageView.image = [UIImage imageNamed:carTemp.icon]; return cellTemp; }
-
day08 - UITableView-02模型优化
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 掌握 设置UITableView的dataSource、delegate UITableView多组数据和单组数据...