根据UIPickerView选择更新UITableView内容

时间:2013-04-01 19:35:40

标签: iphone ios objective-c uitableview uipickerview

我有一个View Controller包含一个包含两个部分的tableView。第1节只有一行,第2节有一些数组,对行数没有限制。

当我单击第1部分中的单元格时,我在操作表中显示了一个pickerView。我在pickerView上选择的任何组件都成为第1节单元格上的标题。

现在我的问题,

我的tableView第2节的内容是否可以依赖于第1节中单元格的标题文本?

例如:

if Section 1's text is = "String A", Section 2's contents should be = "Array A"
if Section 1's text is = "String B", Section 2's contents should be = "Array B"
if Section 1's text is = "String C", Section 2's contents should be = "Array C"
if Section 1's text is = "String D", Section 2's contents should be = "Array D"

依旧......

另外,我希望tableView更新其内容,因为我用所需的String解除了pickerView。 非常感谢一些示例代码/参考。

2 个答案:

答案 0 :(得分:0)

一个好的解决方案是创建一个存储用户选择的枚举变量,然后使用它来确定表视图内容。以下是一些示例代码:

typedef enum { SelectionA, SelectionB, SelectionC } Selection;

@interface MyViewController : UITableViewController (UITableViewDataSource, UITableViewDelegate)

@property (nonatomic) Selection selection;
@property (nonatomic, strong) NSMutableArray *arrayA;
@property (nonatomic, strong) NSMutableArray *arrayB;
@property (nonatomic, strong) NSMutableArray *arrayC;

@end


@implementation MyViewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  NSInteger num = 0;
  if(section==0) {
    num = 1;
  }
  else {
    switch(self.selection)
    {
       case SelectionA: num = [self.arrayA count]; break;
       case SelectionB: num = [self.arrayB count]; break;
       case SelectionC: num = [self.arrayC count]; break;
    }
  }

  return num;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  NSString *cellText = nil;
  if(indexPath.section==0) {
    switch(self.selection)
    {
       case SelectionA: cellText = @"String A"; break;
       case SelectionB: cellText = @"String A"; break;
       case SelectionC: cellText = @"String A"; break;
    }
  }
  else {
    switch(self.selection)
    {
       case SelectionA: cellText = [self.arrayA objectAtIndex:indexPath.row]; break;
       case SelectionB: cellText = [self.arrayA objectAtIndex:indexPath.row]; break;
       case SelectionC: cellText = [self.arrayA objectAtIndex:indexPath.row]; break;
    }
  }

  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
        cell = [[UITableViewCell alloc]
           initWithStyle:UITableViewCellStyleDefault
           reuseIdentifier:CellIdentifier];
  }

  cell.textLabel.text = cellText;
}

@end

答案 1 :(得分:0)

用于第1节的集合标题

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   UITableViewCell *cell = (UITableViewCell *)sender.superview;
   cell.textLabel.text = [NSString stringWithFormat:@"%@",[self.listOfPickerData objectAtIndex:row]];

    [self customeMethod];
  [self.tabelView reloadData];
}

表示基于所选piker视图行的数组

-(void)customeMethod
{
    int row = [repeatPickerView selectedRowInComponent:0];
    self.selectedRowFromPicker = [repeatPickerData objectAtIndex:row];

   if([self.selectedRowFromPicker isEqualToString:@"piker row 1 "])
   {
     // NSArray *myArray1......
   }
   eles if([self.selectedRowFromPicker isEqualToString:@"piker row 2 "])
   {
     // NSArray *myArray2......
   }
   else
   {
     // NSArray *myArray3......
   }
}