简体   繁体   English

根据UIPickerView选择更新UITableView内容

[英]Update UITableView contents depending on UIPickerView selection

I have a View Controller containing a tableView with two sections. 我有一个包含有两个部分的tableView的视图控制器。 Section 1 has only one row, and Section 2 has some array with no limit on the number of rows. 第1节只有一行,第2节有一些数组,行数没有限制。

When I click on the cell in Section 1, I have a pickerView appear in an Action Sheet. 当我单击第1节中的单元格时,我在动作表中出现了pickerView。 Whatever component I select on my pickerView becomes the Title on that Section 1 cell. 无论我在pickerView上选择的任何组件,都将成为该第1节单元格的标题。

Now my question, 现在我的问题是

Can the contents of Section 2 of my tableView depend on the title text of the cell in Section 1? tableView第2节的内容可以取决于第1节中单元格的标题文本吗?

For Example: 例如:

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"

and so on... 等等...

Also, I would like the tableView to update its contents as I dismiss the pickerView with the desired String. 另外,当我用所需的字符串关闭pickerView时,我希望tableView更新其内容。 Some sample code/reference is greatly appreciated. 一些示例代码/参考是极大的赞赏。

One good solution would be to create an enum variable that stores the user's selection, then use that to determine the table view contents. 一个好的解决方案是创建一个存储用户选择的枚举变量,然后使用该变量来确定表视图的内容。 Here is some sample code: 这是一些示例代码:

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

for set title of section 1 用于第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];
}

for your array base on selected piker view's row 基于选定的筹码视图行的数组

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM