无法显示另一个tableview和自定义表格单元格的Tableview

时间:2012-02-08 14:36:51

标签: ios uitableview uistoryboard

从tableview到另一个表视图(使用Xcode 4.2和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;
}

故事板表格视图

这是我得到的错误

  

2012-02-08 22:28:37.719测试[4668:f803]终止应用程序由于   未捕获的异常'NSInternalInconsistencyException',原因:   'UITableView dataSource必须从中返回一个单元格   的tableView:的cellForRowAtIndexPath:“

3 个答案:

答案 0 :(得分:2)

你的cellForRowAtIndexPath方法尝试将可重复使用的单元格出列,但如果找不到则不会创建它(如果没有单元可供重用,则会发生这种情况)

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

答案 1 :(得分:1)

请注意,dequeueReusableCellWithIdentifier:和dequeueReusableCellWithIdentifier:forIndexPath:是不同的方法。

以下链接可能会对您有所帮助。

Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

答案 2 :(得分:0)

我不太确定你在这里要做什么:

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

第一行创建一个名为profiles的新可变数组。第二行创建一个容量为20的自动释放的可变数组,并将其分配给配置文件。所以你基本上掩盖了你在第一行创建的数组。你可以说

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

NSMutableArray *profiles = [NSMutableArray arrayWithCapacity:20];

正如@ wattson12所提到的那样,你崩溃的原因是你要将一个尚未创建的单元格出列。您总是希望尝试将单元格出列,但如果不存在,则需要创建一个单元格。同样,@ wattson12为该任务提供了必要的代码。