2012-02-08 112 views
1

从tableview到另一个table view(使用Xcode 4.2和iOS 5)。Tableview跨越另一个tableview和自定义表格单元格不能显示

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 = @"[email protected]"; 
    [profiles addObject:profile]; 
    profile=[[Profiles alloc]init]; 
    profile.profile_name = @"Jin-Aurora"; 
    profile.biz_type_desc = @"Software"; 
    profile.profile_address = @"682A"; 
    profile.profile_email = @"[email protected]"; 
    [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:'

回答

0

我不太清楚是什么你试图在这里做:

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

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

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

NSMutableArray *profiles = [NSMutableArray arrayWithCapacity:20]; 

你崩溃的原因,如@ wattson12提到的是,你是出队尚未创造了一个小区。你总是想尝试去出队,但如果一个不存在,你需要创建一个。再次,@ wattson12已经为该任务提供了必要的代码。

+0

嗨jmstone,我的代码是这样的。ProfileCell * cell =(ProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil){cell = [[ProfileCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];} ( – 2012-02-09 01:48:20

+0

如果你没有得到任何单元格,在该方法中设置一个断点,看它是否被击中,否则,通常的罪魁祸首是你的数据源说该部分中没有任何行(请参阅numberOfRowsInSection方法返回) – jmstone617 2012-02-09 02:28:51

+0

嗨jmstone,它返回我tableViewCellStyleDefault由于我把initWithStyle,我设置cell.textLabel.text = profile.profile _名称。它能够显示。但我想要显示自定义表格单元:(我应该为InitWithStyle放置自定义表格视图单元格? – 2012-02-09 03:29:10

2

你的cellForRowAtIndexPath方法尝试出队可重复使用的电池,但犯规创建它,如果没有找到它(如果没有可重复使用的细胞将发生)

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

嗨瓦特森,我的代码是这样的。ProfileCell * cell =(ProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil){cellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }但没有显示在桌子上,顺便说一下你如何定义样式? – 2012-02-09 01:29:59

+0

看起来不错,检查以确保该方法被调用,也许[self.profileList计数]返回0.风格是在[UITableView]中定义的(http://www.google.co.uk/url?sa = T&RCT = J&q = UITableView的&源=幅和CD = 1&VED = 0CC8QFjAA&URL = HTTP%3A%2F%2Fdeveloper.apple.com%2Flibrary%2FIOs%2Fdocumentation%2FUIKit%2FReference%2FUITableView_Class%2FReference%2FReference.html&EI = CJUzT-HfD8HH0QW1-7CyAg&USG = AFQjCNFNYcfigQMvMDGR -pLVt9t5jFVlow)docs – wattson12 2012-02-09 09:42:47

+0

hi wattson,我把UITableCellStyleDefault,它显示我默认表格单元格。我错过了一些让自定义表格单元无法显示的东西。任何方式可以帮助? :( – 2012-02-09 11:29:16