Question

I am stuck right now with how to create my UITableView. Right now it works fine with 1 object providing the data for it's rows and cells. The problem is, I have a 2nd object that I also want to use in the table view. Normally, this is very easy and I just create 2 sections in the UITableView and use one object for the first section, and use the second object for the second section.

But for this UITableView, I only want to use 1 section. And I need to programatically populate the row's and cell's with text from BOTH of the objects. I have no idea how to do this though when only using 1 section.

Here are all of my method implementations that help create my UITableView:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


     return 1 ;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return [self.messages count];

}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {


    return @"Inbox Messages";


}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *cellIdentifier = @"SettingsCell";

    UITableViewCell  *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];


    PFObject *message = [self.messages objectAtIndex:indexPath.row];


    UIImage *selectMessageButtonImage = [UIImage imageNamed:@"SliderThumb-Normal-G"];


    UIImage *selectMessageButtonImageHighlighted = [UIImage imageNamed:@"SliderThumb-Normal"];



    UIButton *openMessageButton = [[UIButton alloc]init];

    openMessageButton.frame = CGRectMake(237, -10, 64, 64);

    [openMessageButton setImage:selectMessageButtonImage forState:UIControlStateNormal];
    [openMessageButton setImage:selectMessageButtonImageHighlighted forState:UIControlStateHighlighted];
    [openMessageButton setImage:selectMessageButtonImageHighlighted forState:UIControlStateSelected];

    [openMessageButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];



    openMessageButton.tag = indexPath.row;


    [cell.textLabel setText:[message objectForKey:@"senderName"]];

    NSString *fileType = [message objectForKey:@"fileType"];

    if([fileType isEqualToString:@"image"]) {


        cell.imageView.image = [UIImage imageNamed:@"icon_image"];


    } else {

        //no image
    }

    [cell.detailTextLabel setText:@""];

    [cell.contentView addSubview:openMessageButton];


    return cell;

}

The object that I am currently using with this UITableView is called message. I want to create a 2nd object called message2 and place it's data in the UITableView as well.

I realize that for the numberOfRowsInSection method implementation I could just return the added count's of both objects, but I don't know how to work the 2nd object into the rest of the code.

Was it helpful?

Solution

Why not create one NSArray combining both of the msg arrays? If you are populating your second msg array from somewhere, just wait until that loading is done and then do:

NSArray *bothArray = [msg1 arrayByAddingObjectsFromArray:msg2];

You can use bothArray as the datasource for the TableView, when you have only one populated msg array, bothArray will be just the elements from msg1, when you have populated msg2, just use the code above and then call:

[self.tableView reloadData];

OTHER TIPS

lets assume you have 2 arrays of 2 objects:

NSArray *msg1;
NSArray *msg2;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return msg1.count + msg2.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   int msg1Count = msg1.count;
   if(indexPath.row < msg1Count)
   { 
      //Here you handle object1
      PFObject *currentObject = msg1[indexPath.row];
   }
   else
    {
       //Here you handle object 2
        PFObject *currentObject = msg2[indexPath.row - msg1Count];
     }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top