如何在2个数组中显示表视图控制器中的项目

时间:2012-09-27 06:00:48

标签: iphone objective-c

我是IOS开发的新手。我被困在一个我希望在表视图控制器中显示内容的地方。在第一行中,我将这个名称称为“All Fences”,对于下面的其余行,我有一个包含所有其他名称的数组。

这就是我在做的事情:

-(void) viewDidLoad{
[super viewDidLoad];
array=[[NSMutableArray alloc] init];
[array addObject:@"All Fences"];
[self fetchOtherArray];
}
-(void) fetchOtherArray{
// I have array2 here
}

你能告诉我如何展示一切。

由于

2 个答案:

答案 0 :(得分:0)

如果数组有一个字符串,并且您已在头文件中定义了array2,请执行此操作

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{        
     return  [array2 count]+1;
}

在cellForRowAtIndexPath中使用此检查:

if(indexPath.row=0){
 // set name "All Fences"
  cell.textLabel.text=[NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.row]];
}
else{
  // set from another array
   cell.textLabel.text=[NSString stringWithFormat:@"%@",[array2 objectAtIndex:indexPath.row-1]];
}

答案 1 :(得分:0)

你不能将你的array2合并到array1中吗?或采取一些临时阵列&合并arra1& arra2,这样你就可以更容易地从单个数组中获取值。您可以在didselectrowatindexpath中对单个阵列执行操作。

看看这个,这样你就可以有所了解。

NSMutableArray *arr1 = [[NSMutableArray alloc] init];
[arr1 addObject:@"first"];                             // your first array
NSMutableArray *arr2 = [[NSMutableArray alloc] init];
[arr2 addObject:@"second"];                            // your second array

 [arr1 addObject:[arr2 objectAtIndex:0]];
 NSLog(@"arr1 >> %@",arr1);

// mediator array
NSMutableArray *arr_temp = [[NSMutableArray alloc]initWithObjects:[arr1 objectAtIndex:0],[arr2 objectAtIndex:0] ,nil];
NSLog(@"arr_temp >>%@",arr_temp);

我坚持要将两个数组值存储在临时数组中。就像你在代码中看到的第二种方式一样,如果你不想修改你的array1以便以后重用。