UITableView 数据封装
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
XMGCarGroup *group = self.groups[section];
return group.cars.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
XMGCarGroup *group = self.groups[indexPath.section];
XMGCar *car = group.cars[indexPath.row];
cell.textLabel.text = car.name;
cell.imageView.image = [UIImage imageNamed:car.icon];
return cell;
}
XMGCarGroup *group0 = [[XMGCarGroup alloc] init];
group0.header = @"德系";
group0.footer = @"德系车666";
group0.cars = @[
[XMGCar carWithName:@"奥迪" icon:@"m_9_100"],
[XMGCar carWithName:@"宝马" icon:@"m_3_100"],
[XMGCar carWithName:@"奔驰" icon:@"m_2_100"]
];
@interface XMGCarGroup : NSObject
@property (nonatomic, strong) NSString *header;
@property (nonatomic, strong) NSString *footer;
@property (nonatomic, strong) NSArray *cars;
@end
@interface XMGCar : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *icon;
+ (instancetype)carWithName:(NSString *)name icon:(NSString *)icon;
@end