gpt4 book ai didi

ios - 如何在应用程序启动时预先选择单元格?

转载 作者:行者123 更新时间:2023-11-30 12:32:26 26 4
gpt4 key购买 nike

我有典型的表格 View ,有机会被选择。我想将它们选择为默认值。

import UIKit

class WeekdaysViewController: UITableViewController {
var weekdays: [Int]!

override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
view.backgroundColor = UIColor.white
weekdays = [1, 2, 3, 4, 5, 6, 7]
}

override func viewWillDisappear(_ animated: Bool) {
performSegue(withIdentifier: Id.weekdaysUnwindIdentifier, sender: self)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)

for weekday in weekdays {
if weekday == (indexPath.row + 1) {
cell.accessoryType = UITableViewCellAccessoryType.checkmark
//cell.tintColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
cell.tintColor = UIColor(red: 255.0/255.0, green: 178.0/255.0, blue: 55.0/255.0, alpha: 1.00)
}
}
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!

if let index = weekdays.index(of: (indexPath.row + 1)){
weekdays.remove(at: index)
cell.setSelected(true, animated: true)
cell.setSelected(false, animated: true)
cell.accessoryType = UITableViewCellAccessoryType.none
//cell.tintColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
cell.tintColor = UIColor(red: 255.0/255.0, green: 178.0/255.0, blue: 55.0/255.0, alpha: 1.00)
//cell.backgroundColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
//cell.tintColor = UIColor.white
} else {
//row index start from 0, weekdays index start from 1 (Sunday), so plus 1
weekdays.append(indexPath.row + 1)
cell.setSelected(true, animated: true)
cell.setSelected(false, animated: true)
cell.accessoryType = UITableViewCellAccessoryType.checkmark
//cell.tintColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
cell.tintColor = UIColor(red: 255.0/255.0, green: 178.0/255.0, blue: 55.0/255.0, alpha: 1.00)
//cell.backgroundColor = UIColor(red: 13.0/255.0, green: 211.0/255.0, blue: 90.0/255.0, alpha: 1.00)
//cell.tintColor = UIColor.white
}
}
}

extension WeekdaysViewController {
static func repeatText( weekdays: [Int]) -> String {
if weekdays.count == 7 {
return "Every Day"
}

if weekdays.isEmpty {
return "No repeat"
}

let preferredLanguage = Locale.current.languageCode
let locale = Locale.current.regionCode
var ret = String()
var weekdaysSorted:[Int] = [Int]()

weekdaysSorted = weekdays.sorted(by: <)

if preferredLanguage == "ru" {
for day in weekdaysSorted {
switch day {
case 1:
ret += "Sun "
case 2:
ret += "Mon "
case 3:
ret += "Tue "
case 4:
ret += "Wed "
case 5:
ret += "Thu "
case 6:
ret += "Fri "
case 7:
ret += "Sat "
default:
//throw
break
}
}
} else {
for day in weekdaysSorted {
switch day{
case 1:
ret += "Sun "
case 2:
ret += "Mon "
case 3:
ret += "Tue "
case 4:
ret += "Wed "
case 5:
ret += "Thu "
case 6:
ret += "Fri "
case 7:
ret += "Sat "
default:
//throw
break
}
}
}
return ret
}
}

我已经在 viewDidLoad() 中添加了

weekdays = [1, 2, 3, 4, 5, 6, 7]

它几乎可以工作,但只有当我转向 WeekdaysViewController 时。

如何解决这个问题?

最佳答案

使用此代码段自动选择表格行索引。

Swift 3

tableView.selectRow(at: IndexPath(item: 1, section: 0), animated: true, scrollPosition: .middle)

Sample Image

关于ios - 如何在应用程序启动时预先选择单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43341409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com