无法显示跨另一个表格视图的表格视图和自定义表格单元格

发布于 2025-01-03 20:19:52 字数 2849 浏览 6 评论 0原文

从一个表视图到另一个表视图(使用 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 = @"[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;
}

Storyboard 表视图

这是我得到的错误

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

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 = @"[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;
}

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:'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

邮友 2025-01-10 20:19:52

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

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

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];
孤云独去闲 2025-01-10 20:19:52

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

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

dequeueReusableCellWithIdentifier:forIndexPath 中的断言失败:

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

The following link below may help you.

Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

屌丝范 2025-01-10 20:19:52

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

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

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

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

NSMutableArray *profiles = [NSMutableArray arrayWithCapacity:20];

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文