2013-04-01 113 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...... 
    } 
}