Objective c 使用不同的NSMutableArray加载UITableView
在我的视图中,我有4个选项卡按钮。单击它们后,我将填充4个阵列。现在我需要将这些数组显示到同一个表中。根据按钮的不同,表中的内容应发生变化。如何做到这一点?根据您选择的按钮,加载一张表或另一张表,例如:Objective c 使用不同的NSMutableArray加载UITableView,objective-c,uitableview,nsmutablearray,Objective C,Uitableview,Nsmutablearray,在我的视图中,我有4个选项卡按钮。单击它们后,我将填充4个阵列。现在我需要将这些数组显示到同一个表中。根据按钮的不同,表中的内容应发生变化。如何做到这一点?根据您选择的按钮,加载一张表或另一张表,例如: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. if (
- (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];
要根据按钮重新绘制表格上的信息,请加载一个表格或另一个表格,如:
- (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];
要根据按钮重新绘制表中的信息,必须使用一个主数组,如tableArray1 您必须在tableview委托和数据源方法中使用此tableArray1 您将四个数组作为array1、array2、array3和array4 在viewdidload首次加载时,给出tableArray1=array1 当tabbutton裁剪方法时 好像按钮1 然后 表array1=表array1 [tableview重新加载数据] 如果按钮2 然后 表array1=array2 [tableview重新加载数据] 如果按钮3 然后 表1=表3 [tableview重新加载数据] 如果按钮4 然后 表1=表4 [tableview重新加载数据]
试试这个逻辑吧,我希望它能帮助你你必须使用一个主数组,比如tableArray1 您必须在tableview委托和数据源方法中使用此tableArray1 您将四个数组作为array1、array2、array3和array4 在viewdidload首次加载时,给出tableArray1=array1 当tabbutton裁剪方法时 好像按钮1 然后 表array1=表array1 [tableview重新加载数据] 如果按钮2 然后 表array1=array2 [tableview重新加载数据] 如果按钮3 然后 表1=表3 [tableview重新加载数据] 如果按钮4 然后 表1=表4 [tableview重新加载数据]
尝试此逻辑我希望这将帮助您您应该实现表源委托方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
它们应该根据当前显示的选定阵列返回数据
然后,单击按钮时,调用表的reloadData方法来更新它。您应该实现表源委托方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
它们应该根据当前显示的选定阵列返回数据
然后,当单击按钮时,调用表的reloadData方法来更新它。adduitabarcontrollerdelegate
- (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];
}
添加UITABBARCONTROLLEDELEGATE
- (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];
}