【问题标题】:Tableview across to another tableview and custom table cell cannot be shown无法显示跨到另一个 tableview 的 Tableview 和自定义表格单元格
【发布时间】:2012-02-08 14:36:50
【问题描述】:

从一个表视图到另一个表视图(使用 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 数据源必须返回一个单元格 tableView:cellForRowAtIndexPath:'

【问题讨论】:

    标签: ios uitableview uistoryboard


    【解决方案1】:

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

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

    【讨论】:

    • 嗨,wattson,我的代码是这样的。 ProfileCell *cell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[ProfileCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } 但是桌子上什么也没有,顺便问一下你是怎么定义风格的?
    • 看起来不错,检查以确保正在调用该方法,也许 [self.profileList count] 返回 0。样式在 UITableView 文档中定义
    • 嗨,wattson,我输入了 UITableCellStyleDefault,它显示了默认表格单元格。我错过了一些东西,以至于自定义表格单元格无法显示。有什么办法可以帮助吗? :(
    • 如果您使用自定义表格视图单元格并在 init/layout 子视图中设置视图,那么您放置什么样式并不重要,只需使用默认值即可。我不使用界面生成器,所以不确定您设置单元格的方式是否正确
    • 我在情节提要上自定义了我的表格单元格,将标识符更改为“ProfileCell”,创建一个名为“ProfileCell”的 UITableViewCell 并替换表格单元格的类。我不知道我错过了哪里。除了这种方式,如何在不使用界面生成器的情况下做到这一点?请指教:)
    【解决方案2】:

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

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

    Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

    【讨论】:

      【解决方案3】:

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

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

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

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

      NSMutableArray *profiles = [NSMutableArray arrayWithCapacity:20];
      

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

      【讨论】:

      • 嗨 jmstone,我的代码是这样的。 ProfileCell *cell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[ProfileCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } 但桌子上什么都没有显示 :(
      • 如果你没有得到任何单元格,请在该方法中设置一个断点,看看它是否被命中。如果不是,通常的罪魁祸首是您的数据源说该部分中没有任何行(查看 numberOfRowsInSection 方法返回的内容)。
      • 嗨 jmstone,它返回我 tableViewCellStyleDefault 由于我放入了 initWithStyle,我设置了 cell.textLabel.text = profile.profile_name。它能够显示。但我想显示自定义表格单元格:(我应该在 InitWithStyle 中为我的自定义表格视图单元格添加什么?
      • 当你说自定义表格单元格时,你是从笔尖加载单元格吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多