2017-03-29 70 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