1

インターネットで解決策を探しても役に立たないという問題があります。私はストーリーボードにある Apress の iOS 5 開発の第 10 章を追っています。

この本には単純なコードが欠けていると思いますが、これを初めて経験するので、解決方法がわかりません。

プロジェクトを 2 回再起動しましたが、デバッガーで次のエラーが発生し続けます。

キャッチされていない例外 'NSInternalInconsistencyException' が原因でアプリを終了しています。理由: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

#import "BIDTaskListController.h"

@interface BIDTaskListController ()
@property (strong, nonatomic) NSArray *tasks;
@end

@implementation BIDTaskListController

@synthesize tasks;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tasks = [NSArray arrayWithObjects:
                  @"Walk the dog",
                  @"URGENT:Buy milk",
                  @"Clean hidden lair",
                  @"Invent miniature dolphins",
                  @"Find new henchmen",
                  @"Get revenge on do-gooder heroes",
                  @"URGENT: Fold laundry",
                  @"Hold entire world hostage",
                  @"Manicure",
                  nil];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    self.tasks = nil;
}

///

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

- (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 [tasks count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = nil;
    NSString *task = [self.tasks objectAtIndex:indexPath.row];
    NSRange urgentRange = [task rangeOfString:@"URGENT"];
    if (urgentRange.location == NSNotFound) {
        identifier = @"plainCell";
    } else {
        identifier = @"attentionCell";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
    cellLabel.text = task;

    return cell;
}
@end
4

3 に答える 3

0

実際、元の問題はコードに欠けているものではなく、ストーリーボードの構成に欠けているものだと思います! IIRC の本では、ストーリーボードに 2 つのセル プロトタイプを作成する方法について説明しています。これらの 2 つのセルがストーリーボードのテーブルビュー内に存在し、適切な識別子が設定されている場合、投稿した元のコードは問題なく動作するはずです。

于 2012-05-01T21:06:01.717 に答える
0

コードの欠如:

...

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
    label.tag = 1;
    [cell addSubview:label];
}

UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
cellLabel.text = task;   

....

于 2012-04-23T07:16:53.397 に答える