如何在UITableViewCellAcessoryCheckMark swift中使用该方法?在ObjC中,我很清楚,但斯威夫特不能这样做。我的目的是进行比较,这是真实的chekmark出现。
答案 0 :(得分:4)
在Swift中,引用UITableViewCellAccessoryCheckMark使用
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
在Swift 4/5中,
cell.accessoryType = UITableViewCell.AccessoryType.checkmark
答案 1 :(得分:0)
以下是Swift 3的工作答案:
import UIKit
class ViewController: UITableViewController {
let foods = ["apple", "orange", "banana", "spinach", "grape"]
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return foods.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = foods[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
else
{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}