2017-03-29 107 views
1

此代碼中的兩個錯誤。在多次刪除行的UITableView刪除行中的錯誤

第一次當我刪除行tableview刪除最後一行未選中的行。

第二個錯誤是當試圖刪除多個行時,將刪除成功,但當重新加載tableview只有一行刪除。休息行會再次出現。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
     return result.count; 
    } 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath : indexPath]; 
     if (!(cell == nil)) { 
       name = (UILabel *)[cell viewWithTag:10]; 
       name.text = [result objectAtIndex : indexPath.row]; 
     } 
     name.tag=indexPath.row; 
      return cell; 
    } 
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
     return YES; 
    } 
    - (IBAction)action:(id)sender { 
     [_myTableView setEditing:YES animated:YES]; 
    } 
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return UITableViewCellEditingStyleDelete; 
    } 


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath : indexPath]; 
     if (!(cell == nil)) { 
       name = (UILabel *)[cell viewWithTag:10]; 
       name.text = [result objectAtIndex : indexPath.row]; 
     } 
     name.tag=indexPath.row; 
      return cell; 
    } 
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
     return YES; 
    } 

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { 

      [result removeObjectAtIndex:indexPath.row]; 
      [tableView reloadData]; 
      [spinner startAnimating]; 
      NSString *id = [profile objectForKey:@"user_id"]; 
      NSString *tb =tid; 

      NSString *string = [NSString stringWithFormat:@"userid=%@&topiid=%@",id,tb]; 
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"myDeleteUrl"]]; 

      NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
      [request setHTTPMethod:@"POST"]; 
      [request setHTTPBody : data]; 
      NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration 
defaultSessionConfiguration]]; [[session dataTaskWithRequest : request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 

       NSDictionary *JSON= [NSJSONSerialization JSONObjectWithData :data options :NSJSONReadingMutableContainers error: nil]; 

       NSArray *alertArray = [JSON objectForKey:@"deletebookmark"]; 

       for (NSDictionary *alert in alertArray){ 

        if ([[alert objectForKey:@"status"] isEqualToString:@"done"]) { 
         NSLog(@"done"); 
         [spinner stopAnimating]; 
        } 
        else{ 
         NSLog(@"notDone"); 
         [spinner stopAnimating]; 
        } 
       } 

      }]resume]; 

     } 
    } 
+0

編輯你的問題得當,使其更具可讀性。 – Hasya

+0

我是新的在stackoverflow我不知道該怎麼做。謝謝你.. –

+0

請參考[如何問一個好問題?](// stackoverflow.com/help/how-to-ask)和[如何創建一個最小,完整和可驗證的示例](// stackoverflow.com/help/mcve)。 – Olaia

回答

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath : indexPath]; 

     if (!(cell == nil)) { 
         name = (UILabel *)[cell viewWithTag:10]; name.text = [result objectAtIndex : indexPath.row]; 
        } 
        name.tag=indexPath.row; 
        return cell; 
      } 

在此數據源的方法,你用了「如果」條件,而且由於你的細胞是可重複使用的細胞,所以你必須在此數據源的方法也使用其他部分。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath : indexPath]; 

if (!(cell == nil)) { 
        name = (UILabel *)[cell viewWithTag:10]; name.text = [result objectAtIndex : indexPath.row]; 
       } 
       else{ 

       name.tag=indexPath.row; 
       } 

       return cell; 
     } 

感謝

+0

仍然最後一個單元格正在刪除 –

+0

我說過,你必須使用If Else部分在cellforrowatindexpath數據源方法,並且我舉了一個例子,現在你必須糾正它。 –

+0

在這裏,結果計數可能是你正在使用的數組,所以你必須從該數組中刪除值,否則重新加載後它將再次加載到單元格中。 –

0

嘗試這種方式

[result removeObjectAtIndex:indexPath.row]; 

[tableView reloadData];// comment this line above code 

[spinner stopAnimating]; 

//After that you can add 

[tableView reloadData]; // above stopanimating line. 

添加此cellForRowAtIndexPath方法

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath : indexPath]; 

    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:@"cell"]; 

    } 

    name = (UILabel *)[cell viewWithTag:10]; 

    name.text = [result objectAtIndex : indexPath.row]; 

    name.tag=indexPath.row; 

    return cell; 

} 
+0

仍然相同的問題,我認爲問題是在tableView cellForRowAtIndexPath:節。 Thankyou –

+0

我編輯答案嘗試一次 –

+0

@RajeshDharani請正確格式化您的代碼,使閱讀更容易! :) – Rikh