使用 pickerView 填充多个部分 tableView
Populating multiple sections tableView using pickerView
我有一个表视图,其数据是从一个 pickerView(3 列)中选择的。这是代码:
- (void)viewDidLoad {
[super viewDidLoad];
//PickerView content and sort them for displaying
Number=@[@"Trans",@"2005",@"2006",@"2007",@"2008",@"2009",@"2010",@"2011",@"2012",@"2013",@"2014",@"2015",@"2016"];
Season=@[@"Spring",@"Summer",@"Fall"];
Course=@[@"CHEM1100 General Chem I",@"CHEM2100 General Chem II",@"CHEM3511 Org Chem",@"CHEM3521 Org Chem II"];
Course =[Course sortedArrayUsingSelector:@selector(compare:)];
Number =[Number sortedArrayUsingSelector:@selector(compare:)];
Season =[Season sortedArrayUsingSelector:@selector(compare:)];
_tableView.delegate = self;
_tableView.dataSource = self;
}
为了填充 tableView,我使用了一个按钮,当按钮被按下时,pickerView 的选择将数据发送到 tableView
- (IBAction)addCourse:(UIButton *)sender {
NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc
NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer
NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent];
NSString *num=Number[numRow];
NSString *season=Season[SeaRow];
NSString *course=Course[CourseRow];
NSString *CourseToAdd=[[NSString alloc ]initWithFormat:@"%@ ",course];
NSString *SeasonToAdd=[[NSString alloc ]initWithFormat:@"%@ ",season];
NSString *YearToAdd=[[NSString alloc ]initWithFormat:@"%@ ",num];
[self.msgCourse addObject:CourseToAdd];
[self.msgSeason addObject:SeasonToAdd];
[self.msgYear addObject:YearToAdd];
[_tableView reloadData];
}
为了在 tableView 中显示数据,我使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.textLabel.text=[self.msgCourse objectAtIndex:indexPath.row];
return cell;
}
确定行和节
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//return [self.msgYear count];
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.msgCourse count];
}
运行 很好。输出如附图所示。
这是我的问题:
(1) 我想要在多个部分中显示的 tableView。例如,“2005 Fall”是一个section,在这个section里面,显示的是课程。换句话说,在 pickerview 中选择的前两列将构成部分标题。
(2)如何对各section的结果进行排序?
您可以在其中定义节数和行数,如下所示:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// number of courses?
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ((section == 0) ? 1 : 3);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return appropriate cell(s) based on section
if(indexPath.section == 0)
{
// Return 1 cell
}
else if(indexPath.section == 1)
{
switch(indexPath.row) {
case 0: // Initialize cell 1
break;
case 1: // Initialize cell 2
break;
...
}
}
return cell;
}
以下是学习更多的好教程:
我有一个表视图,其数据是从一个 pickerView(3 列)中选择的。这是代码:
- (void)viewDidLoad {
[super viewDidLoad];
//PickerView content and sort them for displaying
Number=@[@"Trans",@"2005",@"2006",@"2007",@"2008",@"2009",@"2010",@"2011",@"2012",@"2013",@"2014",@"2015",@"2016"];
Season=@[@"Spring",@"Summer",@"Fall"];
Course=@[@"CHEM1100 General Chem I",@"CHEM2100 General Chem II",@"CHEM3511 Org Chem",@"CHEM3521 Org Chem II"];
Course =[Course sortedArrayUsingSelector:@selector(compare:)];
Number =[Number sortedArrayUsingSelector:@selector(compare:)];
Season =[Season sortedArrayUsingSelector:@selector(compare:)];
_tableView.delegate = self;
_tableView.dataSource = self;
}
为了填充 tableView,我使用了一个按钮,当按钮被按下时,pickerView 的选择将数据发送到 tableView
- (IBAction)addCourse:(UIButton *)sender {
NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc
NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer
NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent];
NSString *num=Number[numRow];
NSString *season=Season[SeaRow];
NSString *course=Course[CourseRow];
NSString *CourseToAdd=[[NSString alloc ]initWithFormat:@"%@ ",course];
NSString *SeasonToAdd=[[NSString alloc ]initWithFormat:@"%@ ",season];
NSString *YearToAdd=[[NSString alloc ]initWithFormat:@"%@ ",num];
[self.msgCourse addObject:CourseToAdd];
[self.msgSeason addObject:SeasonToAdd];
[self.msgYear addObject:YearToAdd];
[_tableView reloadData];
}
为了在 tableView 中显示数据,我使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.textLabel.text=[self.msgCourse objectAtIndex:indexPath.row];
return cell;
}
确定行和节
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//return [self.msgYear count];
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.msgCourse count];
}
运行 很好。输出如附图所示。 这是我的问题:
(1) 我想要在多个部分中显示的 tableView。例如,“2005 Fall”是一个section,在这个section里面,显示的是课程。换句话说,在 pickerview 中选择的前两列将构成部分标题。
(2)如何对各section的结果进行排序?
您可以在其中定义节数和行数,如下所示:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// number of courses?
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ((section == 0) ? 1 : 3);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return appropriate cell(s) based on section
if(indexPath.section == 0)
{
// Return 1 cell
}
else if(indexPath.section == 1)
{
switch(indexPath.row) {
case 0: // Initialize cell 1
break;
case 1: // Initialize cell 2
break;
...
}
}
return cell;
}
以下是学习更多的好教程: