Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-09-2009, 03:03 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
starwarsdevwookie59 is on a distinguished road
Default Multiple Custom Cells in one Table

Im trying to make a table with headers and different types of cells that are made in ib... I know how to do it with one customcell but how can i use different ones?

header---
custom1---
custom2---

header---
custom3---
custom3---

etc... Any help would be great thanks!
__________________
Check out our Apps!
Boxen4Oxen
VisCenter

Film Budget

Everwell TV
starwarsdevwookie59 is offline   Reply With Quote
Old 09-09-2009, 03:11 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Switch off using the indexPath, use multiple identifiers, etc. The process really isn't any different.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-09-2009, 03:17 PM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

I'll assume you know how to create multiple custom cells. This is how you use them now...

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString* [] cellIdentifiers = { @"cellID1" , @"cellID2", ... };
      
     UITableViewCell* cell;
     int customCellType  = // this is for you to figure out.
     
     cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifiers[ customCellType ]];
 
     if( cell ==nil )
     {
        switch( customCellType )
        {
          case 0:
               cell = [[[CustomCell1 alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier: cellIdentifiers[customCellType] ] autorelease];
               break;
          case 1:
               cell = [[CustomCell2 alloc] ...
               break;
          }
      }
   }      
}
smithdale87 is offline   Reply With Quote
Old 09-12-2011, 10:11 PM   #4 (permalink)
Rock On!
 
dana0550's Avatar
 
Join Date: Apr 2011
Location: U.S.A.
Posts: 89
dana0550 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Switch off using the indexPath, use multiple identifiers, etc. The process really isn't any different.
Can you please clarify what you mean? I have tried using multiple identifiers and I am still not able to cell multiple custom cells to appear.

Thanks,
Dana

Code:
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier1 = @"Apple";
    Apple *cell1 = (Apple *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    
    static NSString *CellIdentifier2 = @"Banana";
    Banana *cell2 = (Banana *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    
    switch (indexPath.row) {
        case 0:
            [[NSBundle mainBundle] loadNibNamed:@"Apple" owner:self options:nil];
            cell1 = [self appleCell];
            [self setappleCell:nil];
            break;
        case 1:
            [[NSBundle mainBundle] loadNibNamed:@"Banana" owner:self options:nil];
            cell2 = [self bananaCell];
            [self setbananaCell:nil];
            break;
            
        default:
            break;
    }
    return cell1;
}
__________________
Appadana Development
dana0550 is offline   Reply With Quote
Old 09-12-2011, 10:56 PM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You never return cell2.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-12-2011, 11:14 PM   #6 (permalink)
Rock On!
 
dana0550's Avatar
 
Join Date: Apr 2011
Location: U.S.A.
Posts: 89
dana0550 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
You never return cell2.
Thanks for the response Brian,

I added return cell2;

I'm still getting the error:

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath
__________________
Appadana Development
dana0550 is offline   Reply With Quote
Old 09-12-2011, 11:15 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

That means cell1 or cell2, whichever is being returned, is nil.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-12-2011, 11:23 PM   #8 (permalink)
Rock On!
 
dana0550's Avatar
 
Join Date: Apr 2011
Location: U.S.A.
Posts: 89
dana0550 is on a distinguished road
Default

Interesting, I thought that was the case but when I try the two nib files separately, meaning I set the number of table rows to 1 they work.

If I change the row number to 2 and put the switch statement or an if statement I get the nil error.

It is a problem when I am trying to customize more than one row, there must be a problem in the way I am trying to add the custom table view cells in my code.

Thanks again.
__________________
Appadana Development
dana0550 is offline   Reply With Quote
Old 09-12-2011, 11:46 PM   #9 (permalink)
Rock On!
 
dana0550's Avatar
 
Join Date: Apr 2011
Location: U.S.A.
Posts: 89
dana0550 is on a distinguished road
Default

Brian,

I decided to try what the apple docs stated:

Code:
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == 0) return appleCell;
    if([indexPath row] == 1) return bananaCell;
    return nil;
}
This worked fine for me, I wish I used this about 4 hours ago, I would not have wasted so much time. The only reason I hesitated to use it was because I thought I would not be able to use separate class files for each custom cell. I need to have to class files because they will be performing very unique tasks.

Thanks again for your help and patience. The good news is this solution is very clean and easy to manage.

Dana
__________________
Appadana Development
dana0550 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 336
11 members and 325 guests
givensur, glenn_sayers, guusleijsten, ipodphone, jbro, mediaspree, mottdog, mtl_tech_guy, Punkjumper, vilisei, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:30 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0