ShurupuS
ShurupuS

Reputation: 2923

UISwitch chang status after reusing the cell

Hi I have a UISwitch on the prototype cell.

When I change the UISwitch to OFF and move the cell out of the UITableView frame by scrolling and then scroll back - the UISwitch is ON.

But the changes were made - because when I again call the UITableView controller to be shown on the screen - the UISwitch is OFF.

The code is:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingsOptionCell * cell = [tableView dequeueReusableCellWithIdentifier: @"SettingsOptionCell"];
    NSDictionary *data = [NSDictionary new];
    cell.onOption.hidden = NO;

    switch (indexPath.section) {

        case SettingsControllerServiceSection:
            data = self.serviceAttr[indexPath.row];
            break;

        case SettingsControllerStatusSection:
            data = self.statusAttr[indexPath.row];
            break;

        default:
            break;
    }

    cell.captionLabel.text = data[@"name"];
    cell.onOption.on = [data[@"check"] boolValue];
    cell.optionId = data[@"id"];
    cell.updateHandler = ^(){
        if (self.updateHandler) {
            [self update];
            self.updateHandler();
        }
    };
    return cell;
}

Upvotes: 0

Views: 271

Answers (1)

HereTrix
HereTrix

Reputation: 1417

Very interesting code. At first: I do not see where You trying to create cell (dequeueReusableCellWithIdentifier: only picks object from reusable queue and I do not see where You put it). At second:

cell.onOption.on = [data[@"check"] boolValue];

Each time when cell should be placed at tableView You set value of switch from dictionary. Are You shure that You change this value?

P.S. Each time when cell should become visible (You scrolling table) will be called

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

Upvotes: 1

Related Questions