2012-06-02 34 views
2

我已經搜索和搜索,我似乎無法找到我的問題的答案。我有一個動態tableview與3行(每行是一個部分)和一個編輯按鈕在桌面右上角。每次用戶點擊編輯時,都必須添加或刪除一行。一切正常,除了+按鈕粘貼到一個新行的部分。這是我的代碼:在多個部分動態添加和刪除行

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

return 3; 
} 


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

int count = [myarray count]; 
if (myarray != nil) count ++; 
return count; 

} 


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

{  

id cell; 
switch(indexPath.section) { 
    case 0: 
     if(indexPath.row==0) { 
      static NSString *cellType1 = @"cellType1"; 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType1]; 
      return cell; 
     } 
     break; 

    case 1: 
     if(indexPath.row==0) { 
      static NSString *cellType2= @"cellType2"; 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType2]; 
      return cell; 
     } 
     break; 

    case 2: 
     if(indexPath.row==0) { 
      static NSString *cellType3= @"cellType3"; 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType3]; 
      return cell; 
     } 
     break; 
    default: 
     break; 
} 
return cell; 
} 



- (IBAction) EditTable:(id)sender 
{ 
if(self.editing) 
{ 
    [super setEditing:NO animated:NO]; 
    [Table setEditing:NO animated:NO]; 
    [Table reloadData]; 
    [self.navigationItem.rightBarButtonItem setTitle:@"Edit"]; 
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain]; 
} 
else 
{ 
    [super setEditing:YES animated:YES]; 
    [Table setEditing:YES animated:YES]; 
    [Table reloadData]; 
    [self.navigationItem.rightBarButtonItem setTitle:@"Ok"]; 
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; 
} 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView   editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone; 

if (self.editing && indexPath.row == ([myarray count])) 
{ 
    return UITableViewCellEditingStyleInsert; 
} else 
{ 
    return UITableViewCellEditingStyleDelete; 
} 
return UITableViewCellEditingStyleNone; 
} 

- (void)tableView:(UITableView *)TableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if (editingStyle == UITableViewCellEditingStyleDelete) 
{ 
    [myarray removeObjectAtIndex:indexPath.row]; 
    [Table reloadData]; 
} else if (editingStyle == UITableViewCellEditingStyleInsert) 
{ 
    switch(indexPath.section) { 
     case 0: 

      if(indexPath.row==0) { 
       static NSString *cellType1 = @"cellType1"; 
       UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:cellType1]; 
       [arry insertObject:cell atIndex:[myarray count]]; 
       [Table reloadData]; 

      } 
      break; 

     case 1: 
      if(indexPath.row==0) { 
       static NSString *cellType2= @"cellType2"; 
       UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:cellType2]; 
       [arry insertObject:cell atIndex:[myarray count]]; 

      } 
      break; 

     case 2: 
      if(indexPath.row==0) { 
       static NSString *cellType3= @"cellType3"; 
       UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:cellType3]; 
       [arry insertObject:cell atIndex:[myarray count]]; 

      } 
      break; 

     default: 
      break; 
    } 

} 

正如我以前說過,一切正常,直到我按+按鈕(當按下編輯按鈕左側出現)來添加一個新行。然後它顯示一個錯誤:'UITableView dataSource必須從tableView:cellForRowAtIndexPath中返回一個單元格。

我在做什麼錯了?非常感激任何的幫助。

回答

0

首先,你從來沒有真正+alloc-init一個單元格,所以-cellForRowAtIndexPath:是最有可能返回零(-dequeueReusableCellWithIdentifier:不會削減它)。

第二關,比較indexPath.row[myarray count]永遠是真實的,因爲數組和表可以是零基礎,但他們的罪名都沒有。

+0

Hello Codafi。首先,感謝您回答並查看我的問題。讓我看看我是否理解:在我的cellForRowAtIndexPath方法中,我必須分配一個單元格變量是否正確?但我不明白我必須在我的TableView的commitEditingStyle方法中做什麼。你有什麼建議?謝謝 – Japa

+0

嗯,因爲單元格被重用,所以只有當單元格爲零(所以與if語句相比)和'commitEditingStyle:'時,我們'alloc alloc''比較是怪異的。只需比較'[myArray count] -1' – CodaFi