-1

In the source code below I am able to change my checkmark object value, but at the time of reload data in cellForRowAtIndexPath method it shows old data:

//TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

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

        return [self.tableData count];

}

//CellforRowAtIndexpath showing old table data
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView     dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]     initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Recipe *obj = nil;



        obj = self.tableData[indexPath.row];
        cell.textLabel.text = obj.name;

        NSLog(@"Reload Data %@", obj.checkmark);
        if ([obj.checkmark integerValue] == 1)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;

        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }




    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    Recipe *recipe = nil;


        recipe = [self.tableData objectAtIndex:indexPath.row];
        if ([recipe.checkmark integerValue] == 1)
        {

            recipe.checkmark=@"0";
        }
        else
        {

            //recipe.checkmark=@"1";
            for (int i=0; i<[self.tableData count]; i++) {
                if (i==indexPath.row) {
                    recipe.checkmark=@"1";
                }
                else{
                    recipe.checkmark=@"0";
                }

                NSLog(@"PK ! %@", recipe.checkmark);
            }
        }
        //[self.tableData addObject:recipe.checkmark];
        [self.myTable reloadData];


    [self.myTable reloadData];

}
Pravin
  • 41
  • 1
  • 7

1 Answers1

0

The good news is normally this issue is in the cell for row logic, but it looks like you have that portion right. I believe the issue is in your cell for row logic because you are not updating tableData correctly. See extra comments below.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    Recipe *recipe = nil;

    recipe = [self.tableData objectAtIndex:indexPath.row];

    if ([recipe.checkmark integerValue] == 1)
    {
        recipe.checkmark=@"0";
    }
    else
    {

        //This only gets called if current checked cell is already checked
        for (int i=0; i<[self.tableData count]; i++) {
            if (i==indexPath.row) {
                recipe.checkmark=@"1";
            }
            else{
                //This is still the recipe for the cell currently selected
                recipe.checkmark=@"0";
            }

            NSLog(@"PK ! %@", recipe.checkmark);
        }
    }
    //[self.tableData addObject:recipe.checkmark];
    [self.myTable reloadData];


    [self.myTable reloadData];

}

Something like this should fix your issue without changing too much of your code or structure.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    Recipe *recipe = [self.tableData objectAtIndex:indexPath.row];

    if ([recipe.checkmark integerValue] == 1)
    {
        recipe.checkmark=@"0";
    }
    else
    {
        recipe.checkmark=@"1";
    }

    //remove all old checkmarks
    for (int i=0; i<[self.tableData count]; i++)
    {
        //Only update recipes that are not in this row
        if (i!=indexPath.row)
        {
            recipe = [self.tableData objectAtIndex:i];
            recipe.checkmark=@"0";
        }
    }

    [self.myTable reloadData];

}

Another things to consider is instead of using a string that you are converting to an integer that is acting like a BOOL just change recipe's property checkmark to a BOOL. I hope that helps and good luck.

Skyler Lauren
  • 3,792
  • 3
  • 18
  • 30