2012-09-25 66 views
0

我在使用ios中的表格視圖時比較新。我正在嘗試使用不同視圖編輯數據並更新原始視圖中的值。我設置了單元標識符並寫下了以下代碼。從tableView返回單元格時出錯

#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:' 

回答

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; 
     } 
+0

感謝bbodayle .. – pbd

+0

但我想通了......我有看法上的動態數據,因此,我不是應該返回小區。我從控制器中刪除了方法,現在它工作正常。 – pbd

相關問題