0

私はiOSでテーブルビューを使用することに比較的慣れていません。別のビューを使用してデータを編集し、元のビューから値を更新しようとしています。セル識別子を設定し、次のコードを書きました。

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
{ 
    return 1;
}

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

    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NameIdentifier";
    Item *currentItem=[self.items objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
     cell.textLabel.text=currentItem.itemName;    
     return cell;
    }

しかし、次のエラーが表示されます。

NSInternalInconsistencyException', 
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
4

1 に答える 1

2

dequeueReusableCellWithIdentifierセルをデキューできたことを確認する必要があります。毎回セルを返さないため、クラッシュしています。再利用可能なセルをデキューできなかった場合は、新しいセルを作成する必要があります。コードは次のようになります。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"NameIdentifier";
        Item *currentItem=[self.items objectAtIndex:indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)  
           cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];

         // Configure the cell...
         cell.textLabel.text=currentItem.itemName;    
         return cell;
        }
于 2012-09-25T18:21:27.777 に答える