简体   繁体   English

根据UIPickerView更新UITableView单元格

[英]Update UITableView cells depending on UIPickerView

I have an 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
 .
 .
 .
}

Then I have an UITableView 然后我有一个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;
}

So right now the UITableView is filled with the objects from the matches array that I initialize in my viewDidLoad . 所以现在的UITableView充满了从物体matches数组,我在我的初始化viewDidLoad How can I update the cells content depending on the selected row of the UIPickerView ? 如何根据UIPickerView的选定行更新单元格内容?

Simply call [self.tableView reloadData]; 只需调用[self.tableView reloadData]; after you update the matches array. 在更新matches数组之后。

-(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];
}

This assumes that self is both the table view data source/delegate and the picker view data source/delegate 假设self既是表视图数据源/代理又是选择器视图数据源/代理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM