我正在为iPhone开发基于导航的应用程序。目前,我在RootViewController.m类中有一个NSArray,我想传递给SecondViewController.m(第二个屏幕)。
该数组包含的对象包含我希望在每个单元格中显示的NSString名称属性,还包含一个NSString地址属性,我想在同一单元格中的name属性下面以小字体显示。该对象还有一个NSString距离属性,我想在每个单元格的最右侧显示。
我的问题是:
为此你可以在你的SecondViewController类中创建一个属性“secondArray”来合成它并在那里发布它,在你的FirstViewController类中你可以用你想要的内容初始化那个“secondArray”对象
//Do something like this in your FirstViewController.m
SecondViewController *secondVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]
secondVC.secondArray = firstArray; //firstArray would be your local array in FirstViewController
[self.navigationController pushViewController:secondVC animated:YES];
然后要在tableview中显示内容,您必须在reuseTableViewCellWithIdentifier方法中创建一个带有三个标签的自定义单元格,这些标签具有您想要的特定帧。然后在tableviewCellForRowAtIndexPath方法中,您可以通过获取“secondArray”的对象来初始化标签文本。
//您可以编写类似这样的代码
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
[cell setFrame:(CGRectMake(0,0,305,50))];
if([identifier isEqualToString:@"CellIdentifier"])
{
//Creating an image view to load the cell image on a table row
UIImageView *imageView1=[[UIImageView alloc]initWithFrame:CGRectMake(8,8,305,50)];
imageView1.tag = IMAGEVIEW1_TAG;
[imageView1 setImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"AdjustCell.png"]]];
[cell.contentView addSubview:imageView1];
//Creating a label to wite "name" on the imageView1 of the table cell
UILabel *cellText=[[UILabel alloc]initWithFrame:CGRectMake(6,5,310,20)];
cellText.tag = CELLTEXT_TAG;
cellText.backgroundColor=[UIColor clearColor];
cellText.textColor=[UIColor whiteColor];
cellText.font=[UIFont systemFontOfSize:15.0];
[cell.contentView addSubview:cellText];
[imageView1 addSubview:cellText];
//Creating a label to wite distance on the imageView1 of the table cell
UILabel *distanceText=[[UILabel alloc]initWithFrame:CGRectMake(220,27,100,15)];
distanceText.tag = DISTANCETEXT_TAG;
distanceText.backgroundColor=[UIColor clearColor];
distanceText.textColor=[UIColor whiteColor];
distanceText.font=[UIFont systemFontOfSize:12.0];
[imageView1 addSubview:distanceText];
//Creating a label to wite "value" on the imageView2 of the table cell
UILabel *valueText=[[UILabel alloc]initWithFrame:CGRectMake(4,0,150,15)];
valueText.tag = VALUETEXT_TAG;
valueText.backgroundColor=[UIColor clearColor];
valueText.textColor=[UIColor whiteColor];
valueText.font=[UIFont boldSystemFontOfSize:12.0];
UIImageView *imageView2=[[UIImageView alloc]init];
imageView2.tag = IMAGEVIEW2_TAG;
[imageView2 addSubview:partnerText];
[imageView2 addSubview:valueText];
[imageView1 addSubview:imageView2];
[imageView2 release];
[imageView1 release];
[valueText release];
[distanceText release];
[cellText release];
[partnerText release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *identifier = @"CellIdentifer";
cell = [tblView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil)
cell = [self reuseTableViewCellWithIdentifier:identifier withIndexPath:indexPath];
NSMutableDictionary *Store = [self.secondArray objectAtIndex:indexPath.row]; //fetch your data here
UILabel *cellText = (UILabel *)[cell.contentView viewWithTag:CELLTEXT_TAG];
cellText.text = [Store valueForKey:@"name"]; // initialize you data here
UILabel *distanceText = (UILabel *)[cell.contentView viewWithTag:DISTANCETEXT_TAG];
UILabel *valueText = (UILabel *)[cell.contentView viewWithTag:VALUETEXT_TAG];
valueText.text=[[Store valueForKey:@"value"]uppercaseString];
return cell;
}
你应该在firstView中为你的数组创建属性,或者声明这个数组并在appDelegate中创建一个属性。然后简单地创建类的对象(这个属性所属的)。然后只需使用.operator就可以访问那个arry。