0

第一个按钮显示总统列表,当您单击他们时,您会看到他们的维基百科页面。一切正常,直到我添加另一个按钮,让您将页面的语言从英语更改为德语等。我在调试器中不断收到此错误:

-[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061 2012-04-30 17:30:01.293 Presidents[11151:f803] 中的断言失败* 由于未捕获的异常而终止应用程序' NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' * First throw call stack:

我正在阅读一本书教程,我再次认为他们缺少代码。任何想法都会很棒!

编辑:菜鸟继续前进,伙计们,我粘贴了错误的代码部分:这个新部分是我在 BIDMasterViewController 之后添加的自定义弹出框,它工作正常。这个新部分是问题出现的地方:

   #import "BIDLanguageListController.h"
#import "BIDDetailViewController.h"

@interface BIDLanguageListController ()

@end

@implementation BIDLanguageListController

@synthesize languageNames;
@synthesize languageCodes;
@synthesize detailViewController;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.languageNames = [NSArray arrayWithObjects:@"English", @"French",
                          @"German", @"Spanish", nil];
    self.languageCodes = [NSArray arrayWithObjects:@"en", @"fr", @"de", @"es", nil];
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, [self.languageCodes count] * 44.0);

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    self.detailViewController = nil;
    self.languageNames = nil;
    self.languageCodes = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [self.languageCodes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    cell.textLabel.text = [languageNames objectAtIndex:[indexPath row]];

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   detailViewController.languageString = [self.languageCodes objectAtIndex:
                                          [indexPath row]];
}

@end
4

3 回答 3

1

你从来没有真正实例化单元格!网上有很多关于如何重用 UITableViewCells 的优秀文档。

Apple 的 Table View Programming Guide 非常棒,绝对值得一读:http: //developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451 -CH7-SW1

同时,为了快速修复,请将您的方法更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //THIS IS THE IMPORTANT PART YOU ARE MISSING
    if(cell == nil) {
        //AND IF YOU PUT AN NSLOG IN HERE, YOU'LL SEE THIS IS CALLED ABOUT THE
        //NUMBER OF TIMES FOR THE INITIALLY VISIBLE CELLS ON THE SCREEN, PLUS 1 OR 2
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [languageNames objectAtIndex:[indexPath row]];

    return cell;
}
于 2012-04-30T23:06:50.723 回答
0

注意到 Xcode 的模板犯了与教程中提到的相同的错误,我决定进一步调查并得出不同的结论。问题不在于单元格的实例化,而在于 cellIdentifier。当一个人在 .xib类中更改 cellIdentifier 时,就会出现问题,但两者都没有。当第一次加载 .xib 时,显然单元格以 .xib 中指定的标识符排队。然后,当代码运行时,单元格总是可以出队,除非代码坚持使用不同的单元格标识符,例如@"Cell"。检查单元格是否为 nil 并实例化单元格会违背可重用单元格的目的。所以,我的回答是,去掉所有 if(cell == nil)语句,然后检查你的单元格标识符。

于 2012-05-02T14:58:32.560 回答
0

如果一个 UITableViewCell 不能从你的 UITableView 中出列,你必须启动一个 UITableViewCell。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [languageNames objectAtIndex:[indexPath row]];

    return cell;
}
于 2012-04-30T23:06:35.353 回答