Tableview中Dynamic Prototypes动态表的使用

来源:互联网 发布:ubuntu 打开jupyter 编辑:程序博客网 时间:2024/06/06 17:49

Tableview时IOS中应用非常广泛的控件,当需要动态的添加多条不同的数据时,需要用动态表来实现,下面给出一个小例子,适用于不确定Section的数目,并且每个Section中的行数也不同的情况,适合新手。首先,我们来看一下效果图,模拟器上运行的结果:


文件结构:


下面来说实现过程,首先创建出游记录和出差记录的数据模型:

出游记录:Travel.h

@interface Travel : NSObject@property (nonatomic, strong) NSString *country;@property (nonatomic, strong) NSString *time;@property (nonatomic, strong) NSString *expend;@property (nonatomic, strong) NSString *traffic;@end
出差记录:BusinessTravel.h

@interface BusinessTravel : NSObject@property (nonatomic, strong) NSString *country;@property (nonatomic, strong) NSString *time;@property (nonatomic, strong) NSString *expend;@property (nonatomic, strong) NSString *traffic;@property (nonatomic, strong) NSString *travelReason;@end
ViewController中为TableView添加数据:

@interface ViewController ()@property(nonatomic,strong) NSMutableArray *travel;@property(nonatomic,strong) NSMutableArray *businessTravel;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //初始化数组,添加模拟数据    self.travel = [[NSMutableArray alloc] init];    self.businessTravel = [[NSMutableArray alloc] init];        Travel *t1 = [[Travel alloc] init];    t1.country = @"韩国";    t1.time    = @"2016.3.10";    t1.expend  = @"800";    t1.traffic = @"飞机";    [self.travel addObject:t1];        Travel *t2 = [[Travel alloc] init];    t2.country = @"欧洲";    t2.time    = @"2016.3.20";    t2.expend  = @"1000";    t2.traffic = @"飞机";    [self.travel addObject:t2];        BusinessTravel *bt = [[BusinessTravel alloc] init];    bt.country      = @"日本";    bt.time         = @"2016.1.20";    bt.expend       = @"1000";    bt.traffic      = @"飞机";    bt.travelReason = @"考察";    [self.businessTravel addObject:bt];    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];}#pragma mark - Table view data source//设置Section的数目- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {        return (self.travel.count + self.businessTravel.count);    }//设置每个Section的行数,有多少个Section,这个方法就执行多少次- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {        if (section < (self.travel.count)) {   //先往Tableview中添加出游记录,如果是出游记录返回4行,出差记录则返回5行        return 4;    } else {        return 5;    }    }//设置Section的标题<span style="font-family: Arial, Helvetica, sans-serif;">,有多少个Section,这个方法就执行多少次</span>- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {        if (section < (self.travel.count)) {        return @"出游记录";    } else {        return @"出差记录";    }    }//往cell中添加数据- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"reuseIdentifier"];    }        if (indexPath.section < (self.travel.count)) {  //判断Section,如果是出游记录,则有4行,分别添加cell的标题和内容        Travel *travel = [self.travel objectAtIndex:[indexPath section]];        switch (indexPath.row) {            case SELECT_INDEX_COUNTRY:                cell.textLabel.text = @"出游国家";                cell.detailTextLabel.text = travel.country;                break;            case SELECT_INDEX_TIME:                cell.textLabel.text = @"出游时间";                cell.detailTextLabel.text = travel.time;                break;            case SELECT_INDEX_EXPEND:                cell.textLabel.text = @"出游支出";                cell.detailTextLabel.text = travel.expend;                break;            case SELECT_INDEX_TRAFFIC:                cell.textLabel.text = @"出游方式";                cell.detailTextLabel.text = travel.traffic;                break;            default:                break;        }    } else {                                          //添加出差记录数据        BusinessTravel *businessTravel = [self.businessTravel objectAtIndex:[indexPath section]-self.travel.count];        switch (indexPath.row) {            case SELECT_INDEX_BUSINESS_COUNTRY:                cell.textLabel.text = @"出差国家";                cell.detailTextLabel.text = businessTravel.country;                break;            case SELECT_INDEX_BUSINESS_TIME:                cell.textLabel.text = @"出差时间";                cell.detailTextLabel.text = businessTravel.time;                break;            case SELECT_INDEX_BUSINESS_EXPEND:                cell.textLabel.text = @"出差支出";                cell.detailTextLabel.text = businessTravel.expend;                break;            case SELECT_INDEX_BUSINESS_TRAFFIC:                cell.textLabel.text = @"出差方式";                cell.detailTextLabel.text = businessTravel.traffic;                break;            case SELECT_INDEX_TRAVEL_REASON:                cell.textLabel.text = @"出差原因";                cell.detailTextLabel.text = businessTravel.travelReason;                break;            default:                break;        }    }        return cell;}@end








0 0
原创粉丝点击