Loading the UITableView with different NSMutableArrays

久未见 提交于 2019-12-06 14:31:14

问题


I have, in my view 4 tab buttons. Upon clicking them i have 4 arrays getting populated. Now i need to display these array into the same table. depending on the button click the contents in the table should change. How can this be done?


回答1:


add 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];
}



回答2:


Depending on the button selected you load one table or the other, something like:

- (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];
}

All this for all the datasource/delegate of the table view.

And another thing, after pressing the button dont forget to call:

[myTableView reloadData];

To redraw the info on your table depending on the button




回答3:


You have to take one main Array like tableArray1;

You have to use this tableArray1 in tableview delegate and datasource methods.

You have the four arrays take as array1,array2, array3 and array4;

In viewdidload first time give tableArray1=array1;

when tabbutton cliked methods

like if button1

then

tableArray1=array1;

[tableview reloadData];

if button2

then

tableArray1=array2;

[tableview reloadData];

if button3

then

tableArray1=array3;

[tableview reloadData];

if button4

then

tableArray1=array4;

[tableview reloadData];

try this logic i hope this will helps you




回答4:


You should implement the table source delegate methods:

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

They should return the data based upon the currently selected array that you're displaying.

Then, when a button is clicked, call the reloadData method of the table to update it.



来源:https://stackoverflow.com/questions/9664165/loading-the-uitableview-with-different-nsmutablearrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!