帝国cms做企业网站,网页策划方案800字,wordpress做双语网站,泰安网站建设方案书Tableview时IOS中应用非常广泛的控件#xff0c;当需要动态的添加多条不同的数据时#xff0c;需要用动态表来实现#xff0c;下面给出一个小例子#xff0c;适用于不确定Section的数目#xff0c;并且每个Section中的行数也不同的情况#xff0c;适合新手。首先#xf…Tableview时IOS中应用非常广泛的控件当需要动态的添加多条不同的数据时需要用动态表来实现下面给出一个小例子适用于不确定Section的数目并且每个Section中的行数也不同的情况适合新手。首先我们来看一下效果图模拟器上运行的结果 文件结构 下面来说实现过程首先创建出游记录和出差记录的数据模型 出游记录Travel.h interface Travel : NSObjectproperty (nonatomic, strong) NSString *country;
property (nonatomic, strong) NSString *time;
property (nonatomic, strong) NSString *expend;
property (nonatomic, strong) NSString *traffic;end 出差记录BusinessTravel.h interface BusinessTravel : NSObjectproperty (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;endimplementation 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 stylefont-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转载于:https://www.cnblogs.com/ltchu/p/5961565.html