如何在自定义表格单元格中正确对齐以编程方式创建的开关

How to properly align a programmatically created switch in a Custom UITableCell

我正在开发我的第一个个人应用程序,我需要一些指导,了解如何让以编程方式创建的开关出现在我的自定义单元格中(在每个标签)。

请注意,我很确定我没有正确实施开关,因为用户无法实际与之交互,但由于我的主要问题标题中有说明,答案不一定要解决这个问题。

话虽如此,如能就这两点中的任何一点提供指导,我们将不胜感激。

谢谢!

SettingsItem.swift

class SettingsSwitch {

var label : String = "Label"
var active : Bool = false

init(label: String, active: Bool) {
    self.label = label
    self.active = active
}

}

SettingCell.swift

class SettingCell: UITableViewCell {

@IBOutlet var settingsLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}

//MARK: - custom

func setCell(labelText: String, activeBool: Bool) {

    self.settingsLabel.text = labelText

    var newSwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0))
    newSwitch.on = activeBool
    newSwitch.setOn(activeBool, animated: true)
    addSubview(newSwitch)
}
}

TableViewController.swift

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var settings : [SettingsSwitch] = [SettingsSwitch]()

    @IBOutlet var settingsTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.initializeSettings()    
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settings.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell : SettingCell = tableView.dequeueReusableCellWithIdentifier("Cell") as SettingCell

        let settingMenu = settings[indexPath.row]
        cell.setCell(settingMenu.label, activeBool: settingMenu.active)

        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func viewDidAppear(animated: Bool) {
        settingsTable.reloadData()
    }

    // MARK: - custom functions

    func initializeSettings() {

        var coolDownOption = SettingsSwitch(label: "Cool Down Timer", active: false)
        var useDefaultTimes = SettingsSwitch(label: "Use Default Times", active: true)

        settings.append(coolDownOption)
        settings.append(useDefaultTimes)
    }
}

试试这个:

class SettingCell: UITableViewCell {

    @IBOutlet var settingsLabel: UILabel!
    var newSwitch: UISwitch!

    override func awakeFromNib() {
        super.awakeFromNib()

        //The rect does not seems to be correct. This will go way beyond the cell rect.
        //may be use UISwitch(frame:CGRectMake(150, 10, 0, 0))
        newSwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0))
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

    //MARK: - custom

    func setCell(labelText: String, activeBool: Bool) {
        self.settingsLabel.text = labelText
        newSwitch.on = activeBool
        newSwitch.setOn(activeBool, animated: true)

        //Add to contentView instead of self.
        contentView.addSubview(newSwitch)
    }
}

我不确定你为什么不把 UISwitch 放在 xib 中。如果您在 setCell 中创建 UISwitch,它将在重复使用单元格时创建多次。所以最好在awakefromNib()中创建。

此外,最好使用自动布局而不是设置固定矩形。