1
votes

From a tableview to another table view (using Xcode 4.2 and iOS 5).

FirstPage.h

#import "FavoritesController.h"

#import "Profiles.h"

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FavoritesController * favoriteview = [[FavoritesController alloc] init];
    [favoriteview setTitle:@"Favorites"];

    NSMutableArray * profiles = [[NSMutableArray alloc]init ];
    profiles = [NSMutableArray arrayWithCapacity:20];

    Profiles * profile = [[Profiles alloc]init];
    profile.profile_name = @"Woot";
    profile.biz_type_desc = @"Woot 1";
    profile.profile_address = @"123, woot";
    profile.profile_email = @"woot@woot.com";
    [profiles addObject:profile];
    profile=[[Profiles alloc]init];
    profile.profile_name = @"Jin-Aurora";
    profile.biz_type_desc = @"Software";
    profile.profile_address = @"682A";
    profile.profile_email = @"jin@jin.biz";
    [profiles addObject:profile];

    [self.navigationController pushViewController:favoriteview animated:YES];
    favoriteview.profilelist = profiles; 
}

FavoriesController.h

@interface FavoritesController : UITableViewController

@property(nonatomic,strong)NSMutableArray * profilelist;

@end

FavoriteController.m

 #import "FavoritesController.h"
 #import "Profiles.h"
 #import "ProfileCell.h"

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return [self.profilelist count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ProfileCell";

    ProfileCell *cell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Profiles * profile = [self.profilelist objectAtIndex:indexPath.row];

    cell.nameLabel.text = profile.profile_name;
    cell.biztypeLabel.text = profile.biz_type_desc;

    // Configure the cell...

    return cell;
}

Storyboard Table View

This is the error I got

2012-02-08 22:28:37.719 test[4668:f803] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

3

3 Answers

2
votes

your cellForRowAtIndexPath method attempts to dequeue a reusable cell, but doesnt create it if it is not found (which will happen if there are no cells available to reuse)

ProfileCell *cell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
    cell = [[[ProfileCell alloc] initWithStyle:style reusueIdentifier:CellIdenfitier] autorelease];
1
votes

Notice that dequeueReusableCellWithIdentifier: and dequeueReusableCellWithIdentifier:forIndexPath: are different methods.

The following link below may help you.

Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

0
votes

I'm not quite sure what you're trying to do here:

NSMutableArray * profiles = [[NSMutableArray alloc]init ];
profiles = [NSMutableArray arrayWithCapacity:20];

The first line creates a new mutable array called profiles. The second line creates an autoreleased mutable array with capacity 20 and assigns it to profiles. So you're basically covering up the array you created in the first line. You can either say

NSMutableArray *profiles = [[NSMutableArray alloc] initWithCapacity:20];

or

NSMutableArray *profiles = [NSMutableArray arrayWithCapacity:20];

The reason you're crashing, as mentioned by @wattson12 is that you are dequeueing a cell that hasn't been created. You always want to try and dequeue a cell, but if one doesn't exist, you need to create one. Again, @wattson12 has provided the necessary code for that task.