2013-04-01 4 views
0

У меня есть контроллер просмотра, содержащий tableView с двумя разделами. Раздел 1 имеет только одну строку, а раздел 2 имеет некоторый массив без ограничения количества строк.Обновление содержимого UITableView в зависимости от выбора UIPickerView

Когда я нажимаю на ячейку в Разделе 1, у меня есть pickerView, который появляется в Листе действий. Какой бы компонент, который я выбираю на моем pickerView, не становится заголовком в этой ячейке Секции 1.

Теперь мой вопрос,

Может содержание раздела 2 моего Tableview зависит от текста заголовка ячейки в разделе 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

Одним из эффективных решений было бы создать переменную enum, которая хранит выбор пользователя, а затем использовать ее для определения содержимого табличного представления. Вот некоторые примеры кода:

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

для базового массива на строке выбранного вида Пикер в

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