在UITableView单元格中使用UISwitch

83

我如何将UISwitch嵌入UITableView单元格中?可以在设置菜单中看到示例。

我当前的解决方案:

UISwitch *mySwitch = [[[UISwitch alloc] init] autorelease];
cell.accessoryView = mySwitch;

3
你目前的做法有什么问题? - MobileMon
6个回答

194

通常情况下,将其设置为accessoryView是最好的选择。您可以在tableView:cellForRowAtIndexPath:中进行设置。当切换按钮被翻转时,您可能想使用目标/操作来执行某些操作。像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch( [indexPath row] ) {
        case MY_SWITCH_CELL: {
            UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
            if( aCell == nil ) {
                aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease];
                aCell.textLabel.text = @"I Have A Switch";
                aCell.selectionStyle = UITableViewCellSelectionStyleNone;
                UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
                aCell.accessoryView = switchView;
                [switchView setOn:NO animated:NO];
                [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
                [switchView release];
            }
            return aCell;
        }
        break;
    }
    return nil;
}

- (void)switchChanged:(id)sender {
    UISwitch *switchControl = sender;
    NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );
}

1
应该使用相应的单元格编号,而不是MY_SWITCH_CELL。很好的全面解决方案! - testing
1
@Jesse 'aCell.accessoryView = switchView;' 的效果和 '[aCell setAccessoryView:switchView];' 是一样的。你避免使用点语法有什么原因吗? - zpasternack
1
非常感谢您的回答!将开关添加为子视图会破坏语音命令。将其设置为附件视图可与VoiceOver完美配合! - Nitin Alabur
2
如何知道所选开关的索引? - doxsi
2
@doxsi switchView.tag = indexPath.row 用于检测哪一行的开关发生了变化,适用于Swift。 - Nazmul Hasan
显示剩余3条评论

10
你可以将UISwitch或任何其他控件添加到单元格的accessoryView中。这样它将显示在单元格的右侧,这可能是你想要的。

9
if (indexPath.row == 0) {//If you want UISwitch on particular row
    UISwitch *theSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
    [cell addSubview:theSwitch];
    cell.accessoryView = theSwitch;
}

1
为什么要使用 initWithFrame?为什么要使用 addSubviewswitch 不能用作变量名。 - testing
抱歉更改了变量名称。我有一些代码,只是在其中更改了变量名称。 - k-thorat
它对我起作用了。少量代码,有效的解决方案。 - Kenan Karakecili
2
我只需设置单元格的accessoryView属性就能完成这项工作。我认为不必将开关添加为子视图。 - johnnieb

2
您可以在Interfacebuilder中准备单元格,将其链接到Viewcontroller的IBOutlet,并在tableview请求适当行时返回它。
或者,您可以为单元格创建一个单独的xib(再次使用IB),并在单元格创建时使用UINib进行加载。
最后,您可以通过编程方式创建开关并将其添加到单元格内容视图或附加视图中。
哪种方法最适合您主要取决于您想要做什么。如果您的表格视图内容固定(例如设置页面等),前两个可能效果很好;如果内容是动态的,则我更喜欢编程解决方案。请更具体地说明您想要做什么,这将使回答您的问题更容易。

我更喜欢编程解决方案(尽管它是用于设置页面),但我也对前两个选项的工作方式感兴趣。也许您可以更详细地解释一下它们。 - testing

1

针对Swift用户

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "TableIdentifer")
        let aswitch = UISwitch()
        cell.accessoryView = aswitch 
}

1
这段代码是有效的,但需要注意不能使用变量名“switch”,因为它被保留用于Switch语句。因此,使用其他名称如“aSwitch”等都是可以的。 - Adam Ware

1

这是一个更完整的解决方案,其中关闭和打开在视图层(UITableViewCell)上发生,并通过 didSelectdidDeselect 将事件转发给tableView代理:

class CustomCell: UITableViewCell {
    private lazy var switchControl: UISwitch = {
        let s = UISwitch()
        s.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
        return s
    }()

    override func awakeFromNib() {
        self.accessoryView = switchControl
        self.selectionStyle = .none // to show the selection style only on the UISwitch
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        (self.accessoryView as? UISwitch)?.isOn = selected
    }

    @objc private func switchValueDidChange(_ sender: UISwitch) { // needed to treat switch changes as if the cell was selected/unselected
        guard let tv = self.superview as? UITableView, let ip = tv.indexPath(for: self) else {
            fatalError("Unable to cast self.superview as UITableView or get indexPath")
        }
        setSelected(sender.isOn, animated: true)
        if sender.isOn {
            tv.delegate?.tableView?(tv, didSelectRowAt: ip)
        } else {
            tv.delegate?.tableView?(tv, didDeselectRowAt: ip)
        }
    }
}

而且在您的代表处


func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
    return false // to disable interaction since it happens on the switch
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // to make sure it is rendered correctly when dequeuing:
    // stuff
    if isSelected { // stored value to know if the switch is on or off
        tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
    } else {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    // more stuff
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // do your thing when selecting
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    // do your thing when deselecting
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接