0

以下は、テーブルビューセルをnibファイルからテーブルビューに配置するコードとして使用されています。テーブルビューと同じニブでテーブルビューセルを取得しました。viewdidloadでもセルを初期化しようとしましたが、うまくいきませんでした。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"identifier";
    NSLog(@"the cellidentifier is %@", CellIdentifier);
    bookmarksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSLog(@"the cell is %@", bookmarksTableViewCell);
    return bookmarksTableViewCell;
}

私が得るエラーは

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

IB で接続を確認し、それらも再配線しましたが、結果は同じです。セルを NSLog しようとすると、null になります。

4

3 に答える 3

1

あなたはそうする必要があります

static NSString *CellIdenetifier = @"Cell";

BookMarksTableViewCell *cell = (BookMarksTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil){
   cell = [[BookMarksTableViewCell alloc] initWithStyle:UITableViewCellStyleSubTitle reuseIdentifer:CellIdentifier]; 
}

あなたがそれを必要とするかどうかはわかりませんが、私はそれを含めると思いました。

于 2012-07-25T19:06:47.767 に答える
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString *CellIdentifier = @"identifier";
    NSLog(@"the cellidentifier is %@", CellIdentifier);

    bookmarksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (bookmarksTableViewCell == nil){
            //allocate your cell here
    }

    return bookmarksTableViewCell;
于 2012-07-25T19:06:54.140 に答える
1

もちろんそれはnullです。

テーブルビューがキューで使用可能なセルよりも多くのセルを表示する必要がある場合、テーブルビューはnilを返します。

dequeueReusableCellWithIdentifier:

十分な量のセルが割り当てられるまで、新しいセルを作成するようにデータソースに要求します。NULLを確認します。

bookmarksTableViewCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (bookmarksTableViewCell == NULL)
    bookmarksTableViewCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStylePlain reuseIdentifier:cellIdentifier] autorelease];

return cell;

ああ、ps.:インターフェイスビルダーを忘れてください。

于 2012-07-25T19:07:07.607 に答える