根据 UIPickerView 更新 UITableView 单元格
Update UITableView cells depending on UIPickerView
我有一个UIPickerView
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return seasons.count;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([season selectedRowInComponent:0] == 6) {
matches = [[NSMutableArray alloc] initWithCapacity: 20];
[matches insertObject:[NSMutableArray arrayWithObjects:@"aaa", @"bbb", @"1-0", @"15-03-2010", nil] atIndex:0];
} else if
.
.
.
}
然后我有一个UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return matches.count;
}
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.team1.text = matches[indexPath.row][0];
cell.team2.text = matches[indexPath.row][1];
cell.score.text = matches[indexPath.row][2];
return cell;
}
所以现在 UITableView
中充满了我在 viewDidLoad
中初始化的 matches
数组中的对象。如何根据 UIPickerView
的选定行更新单元格内容?
更新 matches
数组后,只需调用 [self.tableView reloadData];
。
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([season selectedRowInComponent:0] == 6) {
matches = [[NSMutableArray alloc] initWithCapacity: 20];
[matches insertObject:[NSMutableArray arrayWithObjects:@"aaa", @"bbb", @"1-0", @"15-03-2010", nil] atIndex:0];
} else if
.
.
.
[self.tableView reloadData];
}
这假设 self
既是 table 视图数据 source/delegate 又是选择器视图数据 source/delegate
我有一个UIPickerView
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return seasons.count;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([season selectedRowInComponent:0] == 6) {
matches = [[NSMutableArray alloc] initWithCapacity: 20];
[matches insertObject:[NSMutableArray arrayWithObjects:@"aaa", @"bbb", @"1-0", @"15-03-2010", nil] atIndex:0];
} else if
.
.
.
}
然后我有一个UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return matches.count;
}
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.team1.text = matches[indexPath.row][0];
cell.team2.text = matches[indexPath.row][1];
cell.score.text = matches[indexPath.row][2];
return cell;
}
所以现在 UITableView
中充满了我在 viewDidLoad
中初始化的 matches
数组中的对象。如何根据 UIPickerView
的选定行更新单元格内容?
更新 matches
数组后,只需调用 [self.tableView reloadData];
。
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([season selectedRowInComponent:0] == 6) {
matches = [[NSMutableArray alloc] initWithCapacity: 20];
[matches insertObject:[NSMutableArray arrayWithObjects:@"aaa", @"bbb", @"1-0", @"15-03-2010", nil] atIndex:0];
} else if
.
.
.
[self.tableView reloadData];
}
这假设 self
既是 table 视图数据 source/delegate 又是选择器视图数据 source/delegate