开发者

Passing an NSArray to a screen and displaying its contents in a table

I am working on a navigation based application for the iPhone. At present, I have an NSArray in the RootViewController.m class, which I want to pass to the SecondViewController.m (the second screen).

The array contains objects that contains an NSString name property which I want to display in each cell, and also contains an NSString address property which I would like to display in small print below the name property, in the same cell. The object also has an NSString distance property which I would like to display on the far right of each cell.

开发者_开发技巧

My question is:

  1. How do I pass the NSArray to the SecondViewController, and display the contents of each index of the NSArray as a cell in a table.
  2. How do I customize the appearance of the cell such that it displays the name as the main element, the address property as a sub element, and the distance property, as another sub element?


For this you can create a property "secondArray" in your SecondViewController class synthesize it and release it there and in your FirstViewController class you can initialize that "secondArray" object with contents you want like

  //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];

And then to display the contents in the tableview you will have to create a custom cell with three labels with specific frames that you want in reuseTableViewCellWithIdentifier method. Then in tableviewCellForRowAtIndexPath method you can initailize the text of the labels by fetching the objects of your "secondArray".

//You can code something like this

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


  1. for first question my answer is property. You can create the property of array.

  2. for second one, You should use the custom Table View cell. It is the best option. you can create your own design of the cell.


You should make property for Your array in firstView or declare this array and make this a property in appDelegate.then simply make object of the class (for which this property belongs).then by simply using .operator you can access that arry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜