2017-04-11 22 views
0

我有典型的表格视图,有机会被选中。我想让他们选为默认。如何在应用程序启动时预先选择单元格?

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

使用此片段自动选择您的表格行索引。

斯威夫特3

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

Sample Image

+0

是否有可能使与对号他们预先选定的? –

相关问题