【问题标题】:Loading the UITableView with different NSMutableArrays使用不同的 NSMutableArrays 加载 UITableView
【发布时间】:2012-03-12 09:03:12
【问题描述】:

在我看来,我有 4 个标签按钮。单击它们后,我将填充 4 个数组。现在我需要将这些数组显示到同一个表中。根据按钮单击表中的内容应该改变。如何做到这一点?

【问题讨论】:

    标签: objective-c uitableview nsmutablearray


    【解决方案1】:

    根据您选择的按钮加载一个或另一个表,例如:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        if (buttonSelected == 1) return [myArray1 count];
    else if (buttonSelected == 2) return [myArray2 count];
    }
    

    所有这些都适用于表格视图的所有数据源/委托。

    还有一件事,按下按钮后别忘了打电话:

    [myTableView reloadData];
    

    根据按钮重绘表格上的信息

    【讨论】:

      【解决方案2】:

      添加UITabBarControllerDelegate

      - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {  
      
      switch (tabBarController.selectedIndex) {
          case 1:
              table_mutableArray=nil;
              [table_mutableArray addObjectsFromArray:tab1_array];
      
              break;
           case 2:
              table_mutableArray=nil;
              [table_mutableArray addObjectsFromArray:tab2_array];
      
              break;
           case 3:
              table_mutableArray=nil;
              [table_mutableArray addObjectsFromArray:tab3_array];
      
              break;
          default:
              table_mutableArray=nil;
              [table_mutableArray addObjectsFromArray:tab4_array];
              break;
      }
      [tableview reloadData];
      }
      

      【讨论】:

        【解决方案3】:

        你必须取一个像 tableArray1 这样的主数组;

        你必须在 tableview 委托和数据源方法中使用这个 tableArray1。

        你有四个数组作为array1,array2,array3和array4;

        在viewdidload第一次给tableArray1=array1;

        tabbutton点击方法时

        like if button1

        然后

        tableArray1=array1;

        [表格视图重载数据];

        如果按钮2

        然后

        tableArray1=array2;

        [表格视图重载数据];

        如果按钮3

        然后

        tableArray1=array3;

        [表格视图重载数据];

        如果按钮4

        然后

        tableArray1=array4;

        [表格视图重载数据];

        试试这个逻辑,希望对你有帮助

        【讨论】:

          【解决方案4】:

          您应该实现表源委托方法:

          - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
          - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
          

          他们应该根据您正在显示的当前选择的数组返回数据。

          然后,当一个按钮被点击时,调用表的reloadData方法来更新它。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-10
            • 1970-01-01
            • 2016-01-27
            • 1970-01-01
            相关资源
            最近更新 更多