У меня есть TableView, данные выбираются из pickerView (3 колонки) .Здесь есть код:Заполнение нескольких разделов Tableview с использованием pickerView
- (void)viewDidLoad {
[super viewDidLoad];
//PickerView content and sort them for displaying
[email protected][@"Trans",@"2005",@"2006",@"2007",@"2008",@"2009",@"2010",@"2011",@"2012",@"2013",@"2014",@"2015",@"2016"];
[email protected][@"Spring",@"Summer",@"Fall"];
[email protected][@"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» - это один раздел, внутри этого раздела показаны курсы. Другими словами, первые два столбца, выбранные в pickerview, образуют заголовок раздела.
(2) Как отсортировать результаты в каждом разделе?
Hello casilas, Спасибо большое за ваш ответ. Одной из сложнейших частей является номер раздела динамика и определяется выбором (первых двух столбцов pickerview) пользователя. Как решить в numberOfSectionsInTableView: пожалуйста? – user4441082