Xcode TableView一次显示到一个不同的数组 [英] Xcode TableView showing to different arrays one at a time PROBLEM

查看:39
本文介绍了Xcode TableView一次显示到一个不同的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道,如果你们不喜欢我的问题,您将投票否决我,并降低我的声誉:(但是我需要学习,也找不到答案,所以在这里我又要冒我的声誉的风险.现在到了重点.我想要做的就是一个带有两个按钮的视图和一个位于按钮下的表格视图,该表格视图需要显示不同的数组,具体取决于单击了女巫按钮,换句话说,我想要做的就是单击按钮1显示array1,如果我单击button2,则显示array2,很简单!但我无法正常工作.这是我的代码示例:

So I know that if you guys don't like my question you are going to vote me down and reduce my reputation :( but I need to learn and can't find a answer so here I'm risking my reputation again. Now to the point. All I'm trying to do is one View with two buttons and a table view under the buttons, this table view needs to display a different array depending witch button was click, in other words all I want it to do is if I click button1 display array1, if I click button2 display array2, simple! but I can't get it to work. Here is a sample of my code:

- (void)viewDidLoad
{

array1 = [[NSArray arrayWithObjects:
               @"A",
               @"B",
               @"c",
               @"D", nil] retain];

array2 = [[NSArray arrayWithObjects:
               @"1",
               @"2",
               @"3",
               @"4", nil] retain];
}

- (IBAction)btnPeriodicos:(id)sender {
displayArray = 1;
}

- (IBAction)btnRadios:(id)sender {
displayArray = 2;
}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 if (displayArray == 1) {
 return [array1 count];
 }
 if (displayArray == 2) {
 return [array2 count];
 }
 [self.tableView reloadData];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor blackColor];
if (menuNumberInt == 1) {
    cell.textLabel.text = [[periodicoArray objectAtIndex:indexPath.row] retain];
}
if (menuNumberInt == 2) {
    cell.textLabel.text = [[radioArray objectAtIndex:indexPath.row] retain];
}

return cell; 
 }

如果我放置而不是if else语句,则numberOfRowsInSection的[array1 count]和cell.textLabel.text = [[periodicoArray objectAtIndex:indexPath.row得以保留];该应用程序会使用该数组填充表格,因此我猜该表格可以工作,但是如何使按钮更改表格单元格呢​​?任何帮助是极大的赞赏.谢谢

If I put instead of the if else statements either [array1 count] for the numberOfRowsInSection and the cell.textLabel.text = [[periodicoArray objectAtIndex:indexPath.row] retain]; the app loads with the table fill with that array so I guess the table works, but how can I make my buttons change the table cells? any help is greatly appreciated. Thanks

推荐答案

您可以为NSArray * currentArray设置一个iVar变量.在viewDidLoad中,您可以定义两个数组(如您当前所做的那样),然后将currentArray分配给array1.

You can have an iVar variable for NSArray *currentArray. In viewDidLoad you can define your two arrays (as you currently do) but then assign currentArray to array1.

然后,更改所有tableView回调以在currentArray之外运行,并摆脱if(displayArray == x)检查.

Then, change all the tableView callbacks to operate off of currentArray and get rid of the if (displayArray == x) checks.

在(IBAction)按钮处理程序中,将currentArray分配给适当的数组,然后调用[tableView reloadData].这将清除表并触发所有tableView回调再次发生.由于currentArray已更改,因此所有回调都将从相应的数组中提取数据.

in the (IBAction) button handlers, assign currentArray to the appropriate array and call [tableView reloadData]. that will clear the table and trigger all the tableView callbacks to happen again. since the currentArray changed, all the callback will pull data from the appropriate array.

希望有帮助.

这篇关于Xcode TableView一次显示到一个不同的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆