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;@endViewController中为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
- Tableview中Dynamic Prototypes动态表的使用
- Static Cells和Dynamic Prototypes的区别
- qt中tableview的使用
- swift中tableview的使用
- Dynamic Initialization动态初始化的使用
- UITableView练习二之Dynamic Prototypes练习
- Prototypes.js 使用
- TableView表视图的使用
- 表视图 TableView 的使用
- .net中dynamic类型的使用
- swift中tableview的使用和注意事项
- Swift中tableView的建立与使用
- iOS 中tableview的使用
- iOS中tableview的使用(1)
- 使用动态类型Dynamic Type
- Dynamic 动态类型 和双问号??的使用
- ibatis使用dynamic配置动态sql时的问题
- 使用Java的Proxy类实现动态代理(Dynamic Proxy)
- leetcode :315. Count of Smaller Numbers After Self :归并排序应用
- ExpandableListView如何进行二级扩展(SimpleExpandableListAdapter)
- Struts2+Jquery+JSON 实现ajax注册信息
- cookie跨域
- 当继承AppCompatActivity使用侧滑时出现白色的问题
- Tableview中Dynamic Prototypes动态表的使用
- HashMap学习
- java.lang.IllegalArgumentException: Service Intent must be explicit
- php crypt in python
- PHP判断字符串是纯英文、纯汉字或汉英混合
- ToggleButton不显示
- ★nyoj304(好题)
- Prohibited package name: java.time
- Intellij下 把项目打包成可执行的jar