0

2 つのセクションを持つ tableView を含むビュー コントローラーがあります。セクション 1 には行が 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"

等々...

また、目的の文字列で pickerView を閉じるときに、tableView の内容を更新したいと思います。いくつかのサンプル コード/リファレンスは大歓迎です。

4

2 に答える 2

0

適切な解決策の 1 つは、ユーザーの選択を格納する 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
于 2013-04-01T19:51:39.060 に答える