简体   繁体   English

在UITableView的错误单元格中出现UISwitch

[英]Appearing UISwitch in wrong cell of UITableView

I have no UISwitch in my first cell, but after scrolling it suddenly appears and I don't understand why?!! 我的第一个单元格中没有UISwitch,但滚动后突然出现,我不明白为什么?!

I would appreciate some advise or even better some coding ideas! 我将不胜感激一些建议甚至更好的一些编码思想!

Thank you! 谢谢!

Screenshots https://www.dropbox.com/sh/qxoc0yidpzxkfgv/gLra_Ilsuv 截图 https://www.dropbox.com/sh/qxoc0yidpzxkfgv/gLra_Ilsuv

-- -

my code 我的密码

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

{
    SettingObject *settingObjectCell = [[[settingsItems objectAtIndex:indexPath.section] valueForKey:@"SectionItems"]objectAtIndex:indexPath.row];


    static NSString *CellIdentifier = @"SettingsCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Selected row - custiomize
    UIView *customColorView = [[UIView alloc] init];


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

    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    // add UISwitcher
    if ([settingObjectCell.switcher isEqualToString:@"YES"] || [settingObjectCell.switcher isEqualToString:@"NO"])

    {

        UISwitch *switcher = [[UISwitch alloc] initWithFrame:CGRectZero];

        //place for switcher
        cell.accessoryView = switcher;


        // load settings or get default
        NSString *switchYesNo = [settingsLoadedUserDefault objectForKey:[settingObjectCell title]] ? [settingsLoadedUserDefault objectForKey:[settingObjectCell title]] : settingObjectCell.switcher ;

        // add value to switcher
        [switcher setOn:[switchYesNo boolValue]];

        // action for switcher
        [switcher addTarget:self action:@selector(updateSwitch:) forControlEvents:UIControlEventValueChanged];

        // selection Turn OFF
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    cell.textLabel.text = [settingObjectCell title];
    cell.detailTextLabel.text = [settingObjectCell subtitle];

    return cell;
}

If the switcher is type of NSString you should use isEqualToString: instead of isEqual:. 如果切换器是NSString类型,则应使用isEqualToString:而不是isEqual:。 Change your if statement to: 将您的if语句更改为:

if ([settingObjectCell.switcher isEqualToString:@"YES"] || [settingObjectCell.switcher isEqualToString:@"NO"])
{
    // Your existing code
}
else
{
    //Move this line here
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // remove switch
    cell.accessoryView = nil;
}

The issue happens because of reuse cell you have to clear the cell (remove switch) when you don't need it. 发生此问题是由于重复使用单元格,您在不需要时必须清除该单元格(删除开关)。 It should work. 它应该工作。

You need to reset your cell on the prepareForReuse method. 您需要在prepareForReuse方法上重置单元格。 When you dequeue the cell from the tableview, the cell is not reset and the old values are still on the cell! 当您从表格视图中退出单元格时,该单元格不会重置并且旧值仍在该单元格上!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM