多次删除UITableView上删除行的错误删除行

时间:2017-03-29 07:02:49

标签: ios objective-c iphone uitableview

此代码中有两个错误。

第一个当我删除tableview中的Row时删除最后一行未选中的行。

第二个错误是当尝试删除多行时,将成功删除但重新加载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];

        }
    }

2 个答案:

答案 0 :(得分: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; 
            }

在这种数据源方法中,您使用了" if"条件,并且由于您的单元格是可重用的单元格,因此您必须在此数据源方法中使用else部分。

- (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; 
        }

由于

答案 1 :(得分: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;

 }