Mukul Chauhan
Mukul Chauhan

Reputation: 13

Bug in Deleting Row on UITableView at Multiple time delete row

Two bug in this code.

First one when i delete Row in tableview delete last row not selected row.

Second Bug is when try to delete more than one row that time will delete successfully but when reload tableview Only one row deleted. Rest row will appear again.

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

        }
    }

Upvotes: 1

Views: 122

Answers (2)

Rajesh Dharani
Rajesh Dharani

Reputation: 285

Try this way

[result removeObjectAtIndex:indexPath.row];

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

[spinner stopAnimating];

//After that you can add

[tableView reloadData]; // above stopanimating line.

Add this cellForRowAtIndexPath method

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

 }

Upvotes: 0

Abhishek Mitra
Abhishek Mitra

Reputation: 3395

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

In this datasource method, you have used a "if" condition, and due to your cell is reusable cell, so you have to use the else part too in this data source method.

Like

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

Thanks

Upvotes: 1

Related Questions