当我在Swift 3.0中的UITableViewController中选择一个新的复选标记时,如何删除默认复选标记?

时间:2017-08-04 09:42:16

标签: ios swift uitableview

目的

我正在使用Swift 3.0和Xcode 8.0制作iPhone应用程序。 现在,我想使用表视图控制器创建一个用于选择颜色的设置菜单。

我想在此菜单中只允许一个单元格(颜色)可选。 我已经将默认选项设置为“绿色”。当用户首先打开设置菜单时,所选单元格(颜色)为绿色。但是用户可以选择一个新的。 (我正在使用UserDefaults来执行此功能。)

问题

当我选择新单元格时,默认单元格不会消失。 我想在选择新单元格时删除此默认选项。 (我认为其他事情都可以。例如,代表,用户默认)

我的想法

我想我需要在didSelectRowAt方法中写一些东西。 也许我需要在选择新签名之前删除所有复选标记。 我在下面的来源中写了评论。

default selection in table view

//  TableViewControllerColorDetail.swift

import UIKit

class TableViewControllerColorDetail: UITableViewController {
    var colors = ["Purple", "White", "Blue", "Green", "Orange", "Yellow", "Red", "Black", "Pink", "Aqua", "Lapis lazuli"]
    var colorname = ""
    let userDefaults = UserDefaults.standard

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.allowsMultipleSelection = false
}

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

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return colors.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ColorsDetail", for: indexPath)

    // Configure the cell...
    cell.textLabel!.text = colors[indexPath.row]

    cell.textLabel?.textColor = UIColor.white

    cell.selectionStyle = UITableViewCellSelectionStyle.none

    let savedcolornumber = userDefaults.object(forKey: "MemoryOfColornumber")
    let colornumber = indexPath.row

    //If user don’t have save data, user is using this App at first time, check to 3rd cell
    if savedcolornumber == nil {
        print("No Save Data")
        if (indexPath.row == 3) {
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }

    //If user have save dete
    } else {
        print("User have a Save Data")
        //Check to Save Color
            if (colornumber == savedcolornumber as! Int) {
                print("It is same as saved color")
                cell.accessoryType = .checkmark
            } else {
                print("It is NOT same as saved color")
                cell.accessoryType = .none
            }
        }

    return cell
}

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

    // (Expect) Delete checkmarks in all cells 
    // ???????

    //  Check to selected cell
    cell?.accessoryType = .checkmark

    // Receive the text from selected cell
    let colorname = colors[indexPath.row]
    let colornumber = indexPath.row

    // Save with the key
    // Reload
    userDefaults.set(colornumber, forKey: "MemoryOfColornumber")
    userDefaults.set(colorname, forKey: "MemoryOfColorname")
    userDefaults.synchronize()

    // Export to AppDelegate
    let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate 
    appDelegate.message = colorname
}

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

    // Delete Checkmark
    cell?.accessoryType = .none
}

/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // Delete the row from the data source
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the item to be re-orderable.
    return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
    */

}

2 个答案:

答案 0 :(得分:3)

您应该存储所选单元格的索引路径,并在didSelectRowAt中删除其复选标记附件。

var selectedIndexPath: IndexPath!

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

    // Delete checkmarks in the previously selected cell
    if indexPath != nil,
        indexPath != selectedIndexPath,
        selectedCell = tableView.cellForRow(at: selectedIndexPath) {
        selectedCell.accessoryType = .none
        selectedIndexPath = indexPath
    }

    //  Check to selected cell
    cell?.accessoryType = .checkmark
}

答案 1 :(得分:3)

如果您不想存储以前检查过的indexPath,那么您可以检查所有行,如下所示。把它放在didSelectRowAt indexPath

for row in 0..<tableView.numberOfRows(inSection: indexPath.section) {
    if let cell = tableView.cellForRow(at: IndexPath(row: row, section: indexPath.section)) {
        cell.accessoryType = row == indexPath.row ? .checkmark : .none
    }
}