UITwitch在UITableView中出现奇怪的显示问题

时间:2014-07-29 18:00:22

标签: ios objective-c uitableview uiswitch

我使用下面的代码将UISwitch添加到UITableView单元格,但是出现了一个奇怪的显示问题。问题似乎发生,因为我会定期自动刷新TableView,从而重新创建UISwitch。

最好显示正在发生的事情。

开始这样: Starts like this

变成这样: Turns into this

顶部开关已打开和关闭。到达此状态需要几分钟(使用黑色模糊),但只要打开UIS开关然后关闭,就可以看到问题。

以下是代码: -

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//Includes code that also adds a UITextView and a UIImageView, but these do not have issues.

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(10, 5.0, 60.0, 50.0)];
[switchView addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged];

[cell.contentView addSubview:switchView];

return cell;

1 个答案:

答案 0 :(得分:4)

每次加载单元格时都会添加一个新开关。你看到的是许多交换机堆叠在一起。

此外,您为交换机设置的目标操作不会像您的选择器名称那样传递索引路径。当我不想继承我的单元格时,我通常使用此解决方案。 Getting data from a textfield inside a prototype uicollectionviewcell

可能的解决方案:使用关联的对象来跟踪您的交换机。

static NSString *kIndexPathKey = @"indexPathKey";
static NSString *kSwitchViewKey = @"switchViewKey"; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(10, 5.0, 60.0, 50.0)];
        [switchView addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
        [self addSwitchView:switchView toCell:cell];
    }

    UISwitch *switchView = [self switchViewForCell:cell];
    [self setIndexPath:indexPath onSwitch:switchView];
    switchView.on = [self isIndexPathSelected:indexPath];

    return cell;
}

- (void)addSwitchView:(UISwitch *)switchView toCell:(UITableViewCell *)cell {
    [cell.contentView addSubview:switchView];
    [cell setAssociatedObject:switchView forKey:kSwitchViewKey];
}

- (UISwitch *)switchViewForCell:(UITableViewCell *)cell {
    return [cell associatedObjectForKey:kSwitchViewKey];
}

- (void)switchValueChanged:(UISwitch *)sender {
    NSIndexPath *indexPath = [self indexPathOfSwitch:sender];
    [self setRowSelected:sender.isOn atIndexPath:indexPath];
    [self.delegate didSelectItem:[self itemAtIndexPath:indexPath] atIndexPath:indexPath selected:sender.isOn sender:self];
}

- (void)setIndexPath:(NSIndexPath *)indexPath onSwitch:(UISwitch *)switchView {
    [switchView setAssociatedObject:indexPath forKey:kIndexPathKey];
}

- (NSIndexPath *)indexPathOfSwitch:(UISwitch *)switchView {
    return [switchView associatedObjectForKey:kIndexPathKey];
}

@implementation NSObject (AssociatedObjects)

- (void)setAssociatedObject:(id)object forKey:(NSString *const)key {
    objc_setAssociatedObject(self, (__bridge const void *)(key), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)associatedObjectForKey:(NSString *const)key {
    return objc_getAssociatedObject(self, (__bridge const void *)(key));
}

@end

请注意,使用关联对象的所有内容都可以通过使用UITableViewCell的自定义子类来完成。但是,子类化有许多优点。

组合物>继承。