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
?
?