【发布时间】:2020-12-16 03:05:10
【问题描述】:
我有一个内置 UISwitch 的工作 UITableView。我希望有一行使用 UITextField。它在第一次显示时有效,但如果我离开屏幕然后回来,标签就会消失,UITextField 会被推到左边。
这是我第一次进入“设置”选项卡时的显示方式:
当我翻到“主页”选项卡(或任何其他选项卡)并返回“设置”选项卡时,我得到以下信息:
我不知道为什么。
这里是有问题的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let defaults = UserDefaults.standard
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath)
var switchTag = 0
var switchState = false
switch indexPath.section {
case SETTINGS_SECTION:
cell.textLabel?.text = settings[indexPath.row].name
switchTag = settings[indexPath.row].tag
switchState = defaults.bool(forKey: "metric")
break
case UPLOADS_SECTION:
cell.textLabel?.text = uploads[indexPath.row].name
switchTag = uploads[indexPath.row].tag
if indexPath.row == 0 {
switchState = defaults.bool(forKey: "strava")
} else if indexPath.row == 1 {
switchState = defaults.bool(forKey: "cycling_analytics")
}
break
case DEVICES_SECTION:
let device = devices[indexPath.row]
cell.textLabel?.text = device.name
switchTag = device.tag
if deviceManager!.savedDevices.firstIndex(of: device.id) != nil {
switchState = true
} else {
switchState = false
}
default:
break
}
if switchTag == 2 {
txtFTP = UITextField(frame: CGRect(x: 300, y: 1, width: 50, height: 50))
//txtFTP = UITextField(frame: .zero)
cell.accessoryView = txtFTP
let defaults = UserDefaults.standard
let ftp = defaults.string(forKey: "FTP")
if ftp == nil {
txtFTP.placeholder = "FTP"
} else {
txtFTP.text = ftp
}
txtFTP.borderStyle = .roundedRect
txtFTP.translatesAutoresizingMaskIntoConstraints = false
txtFTP.font = UIFont.systemFont(ofSize: 15)
txtFTP.addTarget(self, action: #selector(valueChanged), for: .editingChanged)
txtFTP.delegate = self
txtFTP.keyboardType = .numberPad
cell.accessoryView = txtFTP
} else {
let switchView = UISwitch(frame: .zero)
switchView.setOn(switchState, animated: true)
switchView.tag = switchTag // for detect which row switch Changed
switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
cell.accessoryView = switchView
}
return cell
}
我首先按照这里的指南解决了这个问题:https://programmingwithswift.com/create-a-custom-uitableviewcell-with-swift/
我在故事构建器中创建了一个新的 nib,然后创建了代表该新自定义单元格的 swift 文件(如上面的链接中所做的那样)。
然后我把上面的代码改成:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let defaults = UserDefaults.standard
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath)
var switchTag = 0
var switchState = false
switch indexPath.section {
case SETTINGS_SECTION:
switchTag = settings[indexPath.row].tag
if switchTag == 2 {
if let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as? CustomTableViewCell {
return customCell
}
}
cell.textLabel?.text = settings[indexPath.row].name
switchTag = settings[indexPath.row].tag
switchState = defaults.bool(forKey: "metric")
break
case UPLOADS_SECTION:
cell.textLabel?.text = uploads[indexPath.row].name
switchTag = uploads[indexPath.row].tag
if indexPath.row == 0 {
switchState = defaults.bool(forKey: "strava")
} else if indexPath.row == 1 {
switchState = defaults.bool(forKey: "cycling_analytics")
}
break
case DEVICES_SECTION:
let device = devices[indexPath.row]
cell.textLabel?.text = device.name
switchTag = device.tag
if deviceManager!.savedDevices.firstIndex(of: device.id) != nil {
switchState = true
} else {
switchState = false
}
default:
break
}
let switchView = UISwitch(frame: .zero)
switchView.setOn(switchState, animated: true)
switchView.tag = switchTag // for detect which row switch Changed
switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
cell.accessoryView = switchView
return cell
}
一切都按预期进行。
【问题讨论】:
标签: swift uitableview uitextfield