【问题标题】:UITableView - switch data from two arraysUITableView - 从两个数组切换数据
【发布时间】:2015-04-14 18:08:20
【问题描述】:

我正在开发一个应用程序,其中有一个 UITableView、两个数组和一个 UISegmentedControl。现在我需要在 UISegmentedControl 值为 0 时从数组 1 加载 UITableView 数据,当 UISegmentedControl 值为 1 时从数组 2 加载数据。简单地说,我需要切换要从数组加载到 UITableView 中的数据。我尝试使用bool,但它不起作用,我也认为它不理想。

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    allTableData = [[NSMutableArray alloc] initWithObjects:
                    [[Food alloc] initWithName:@"BWM" andDescription:@"Auto" andDefinice:@"Osobni"],nil ];

    allTableData2 = [[NSMutableArray alloc] initWithObjects:
                    [[Food alloc] initWithName:@"Renault" andDescription:@"Dodavka" andDefinice:@"Velka"],nil ];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    long rowCount;
    if(self.isSwich == false)
        rowCount = allTableData.count;
    else
        rowCount = allTableData2.count;

    return rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    Food* food;
    if(self.isSwitch  == false)
    {
        food = [allTableData objectAtIndex:indexPath.row];
    }
    else
    {
        food = [allTableData2 objectAtIndex:indexPath.row];
    }
    cell.textLabel.text = food.name;
    cell.detailTextLabel.text = food.description;

    return cell;
}
-(IBAction)switchdata:(id)sender
{
    if(self.myswitcher.selectedSegmentIndex == 0)
    {
        isSwitch = FALSE;
    }
    else
    {
        isSwitch = TRUE;
    }
}

【问题讨论】:

  • 您能否更详细地解释一下“不起作用”部分?
  • 数据不切换。 UITableView 显示来自一个数组的数据,但是当我单击 UISegmentedControl 时没有任何反应。

标签: ios objective-c arrays uitableview


【解决方案1】:

您在witchdata: 实现中缺少对reloadData 的调用。添加此调用将解决问题。

我也觉得不太理想。

没错。与其存储一个标明要用于数据源的数组的标志,不如存储数组本身。这将消除 isSwitch 属性上的所有 ifs:

// Declare this as an instance variable, and use it
// in your data source methods.
NSArray *theSource;

- (void)viewDidLoad {
    [super viewDidLoad];

    allTableData = [[NSMutableArray alloc] initWithObjects:
                    [[Food alloc] initWithName:@"BWM" andDescription:@"Auto" andDefinice:@"Osobni"],nil ];

    allTableData2 = [[NSMutableArray alloc] initWithObjects:
                    [[Food alloc] initWithName:@"Renault" andDescription:@"Dodavka" andDefinice:@"Velka"], nil];
    theSource = allTableData;
}

-(IBAction)switchdata:(id)sender {
    if(self.myswitcher.selectedSegmentIndex == 0) {
        theSource = allTableData;
    } else {
        theSource = allTableData2;
    }
    [myTable reloadData];
}

或者,您可以将isSwitch 设为一个整数,并将其用作数组的索引,该数组的索引为0,allTableData 位于索引1,allTableData2 位于索引1:

NSArray *sources;
int sourceIndex;
// Use sources[sourceIndex] as the current source

- (void)viewDidLoad {
    [super viewDidLoad];

    allTableData = [[NSMutableArray alloc] initWithObjects:
                    [[Food alloc] initWithName:@"BWM" andDescription:@"Auto" andDefinice:@"Osobni"], nil];

    allTableData2 = [[NSMutableArray alloc] initWithObjects:
                    [[Food alloc] initWithName:@"Renault" andDescription:@"Dodavka" andDefinice:@"Velka"],nil ];
    sources = @[allTableData, allTableData2];
    sourceIndex = 0;
}

-(IBAction)switchdata:(id)sender {
    sourceIndex = self.myswitcher.selectedSegmentIndex;
    [myTable reloadData];
}

【讨论】:

    【解决方案2】:

    首先,在您的 switchData: 方法中 - 使用 self.switch 访问它的 setter、getter。

    其次,不要使用布尔值来跟踪段值,而是使用直接段值。 用表视图委托、数据源方法中的 self.switcher.selectedSegmentIndex 替换您的 self.switch 值。

    最后,在你的 switchData: 方法中重新加载表格视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多