无法显示跨另一个表格视图的表格视图和自定义表格单元格
从一个表视图到另一个表视图(使用 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
- Picture of prototype cell xib with Identity inspector
- Picture of prototype cell xib with Attributes inspector
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 cellForRowAtIndexPath 方法尝试将可重用单元出队,但如果找不到可重用单元,则不会创建它(如果没有可重用单元,则会发生这种情况)
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)
请注意, 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:
我不太确定你想在这里做什么:
第一行创建一个名为 profile 的新可变数组。第二行创建一个容量为 20 的自动释放的可变数组,并将其分配给配置文件。所以你基本上覆盖了你在第一行创建的数组。您可以说
或
您崩溃的原因,正如 @wattson12 提到的那样,您正在使尚未创建的单元出队。您总是想尝试使单元出列,但如果单元不存在,则需要创建一个。同样,@wattson12 提供了该任务所需的代码。
I'm not quite sure what you're trying to do here:
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
or
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.