【问题标题】:How to reload tableview data one array to another array?如何将一个数组的tableview数据重新加载到另一个数组?
【发布时间】:2023-03-22 05:51:01
【问题描述】:

我正在尝试使用双 NSMutableArray 值创建单个 table view。现在我维护UISegmentcontrol第一个按钮单击以加载第一个NSMutableArray数据。第二个按钮点击加载第二个NSMutableArray数据值(点击第二个按钮后第一个数据不应该显示)。

NSMUtableArray *firstarray = [First values];
NSMUtableArray *secondarray = [second values];


-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
    switch (segment.selectedSegmentIndex) {
        case 0:{
            //action for the first button (Current)
           //want to load first array data into table
            break;}
        case 1:{
            //action for the first button (Current)
           //want to load second array data into table
           break;}
    }
}

【问题讨论】:

    标签: ios objective-c arrays uitableview


    【解决方案1】:

    创建 1 个数组来保存表格的数据,因此您不必在 UITableView 的每个委托方法中检查(使用 if,...):

    NSMutableArray * arrTableData;
    
    -(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
    {
        switch (segment.selectedSegmentIndex) {
            case 0:{
                arrTableData = firstarray;
               [tableView reloadData];
    
                break;}
            case 1:{
                 arrTableData = secondarray;
                 [tableView reloadData];
    
               break;}
        }
    }
    

    arrTableData 用于UITableView 的代表:numberOfRowsInSectioncellAtIndex...

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    
       return arrTableData.count;
    
    }
    

    【讨论】:

    • 但我第一次做什么?在选择按钮之前,我需要列出 defaulty firstarray data@anthu
    • 是的,您将通过在某处指定arrTableData = firstarray 来选择哪个是默认值。
    • 设置选中段的时候也可以设置数组的初始值
    • 嗨。我在哪里可以为默认 arrTableData = firstarray @anthu 分配这个
    • viewDidLoad ,或者你第一次得到firstarray 的地方,或者在你第一次加载表格之前。
    【解决方案2】:

    您可以使用枚举来检查您按下了哪个按钮。对于按钮一,将枚举值设置为 1,对于按钮二,将枚举值设置为 2。在表视图委托方法中,您可以检查当前选定的枚举并返回适当的值。

    例如:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = 0;
    
    
    if(m_choice == 1)
        count = [firstarray count];
    else if(m_choice == 2)
        count = [secondarray count];
    
    return count;}
    

    对其他表格视图方法也这样做。

    【讨论】:

    • 虽然这会起作用,但它有很多重复的代码,而另一个答案是一种更优雅的方法
    【解决方案3】:

    通过以下方式实现您的UITableViewDelegate

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
       if (mySegmentContorll.selectedSegmentIndex == 0) {
           // Draw Cell content with data taken from first array, data = firstarray[indexPath]
       } else if (mySegmentContorll.selectedSegmentIndex == 1) {
          // Draw Cell content with data taken from second array, data = secondarray[indexPath]
       }
    
    }
    

    当分段控制发生变化时也调用[tableView reloadData];

    【讨论】:

      【解决方案4】:

      创建一个布尔变量,比如 isLoadFirst 然后在您的以下代码中

      -(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
      {
      switch (segment.selectedSegmentIndex) {
          case 0:{
               isFirstLoad  = YES;
               [yourTableView reloadData];
              //action for the first button (Current)
             //want to load first array data into table
              break;}
          case 1:{
              //action for the first button (Current)
             //want to load second array data into table
              isFirstLoad  = NO;
               [yourTableView reloadData];
             break;}
      }
      }
      

      现在在你的

       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
           if (isFirstLoad)
         return firstArrayCount;
         else 
         return secondArrayCount
      }
      

      同样的检查

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      

      【讨论】:

      • 谢谢你,Arsian。它也很好的答案!
      猜你喜欢
      • 2012-09-11
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 2022-01-13
      • 2012-03-27
      相关资源
      最近更新 更多