我有典型的表格视图,有机会被选中。我想将它们选为默认值。
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
时。
如何解决这个问题?
答案 0 :(得分:0)
使用此代码段自动选择表格行索引。
Swift 3
tableView.selectRow(at: IndexPath(item: 1, section: 0), animated: true, scrollPosition: .middle)