2013-04-01 116 views
0

我有一个包含两个部分的tableView的视图控制器。第1节只有一行,第2节有一些没有数量限制的数组。根据UIPickerView选择更新UITableView内容

当我点击第1节中的单元格时,我有一个pickerView出现在一个Action Sheet中。无论我在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如我所需的字符串驳回pickerView来更新其内容。 一些示例代码/参考非常感谢。

回答

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 
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...... 
    } 
}