繁体   English   中英

如何在Swift中的Table View Cell中进行有限的多个复选标记?

[英]how to make limited multiple checkmark in Table View Cell in Swift?

在此处输入图片说明

我是编程和iOS开发方面的新手,我需要制作具有多个受限复选标记的表格视图。

我的意思是,我想允许用户在表格视图中最多选择3个项目(不仅可以选择1个项目,而且不能选择表格视图中的所有项目),我已经尝试过,但是没有得到想要的项目,我只能在表格视图中选择一项

这是我使用的代码

import UIKit

class CreateEventStep2VC: UIViewController {

    @IBOutlet weak var eventTypeNameLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!

    var newEvent : [String:Any]!
    var eventTypeAvailableData = [String]()
    var selectedEventTypes = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // initial value
        eventTypeNameLabel.text = ""

        // get event Type Data list from EventType data model
        eventTypeAvailableData = EventType.allValues.map { $0.toString() }
    }




}

extension CreateEventStep2VC : UITableViewDataSource {

    //MARK: - UITableViewDatasource

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return eventTypeAvailableData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "EventTypeCell", for: indexPath) as! CreateEventStep2Cell
        cell.eventTypeNames = eventTypeAvailableData[indexPath.row]
        return cell
    }

}


extension CreateEventStep2VC : UITableViewDelegate {

    //MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .none
        }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark

        }
    }



}

请你帮助我好吗 ?

您不能简单地将复选标记添加到单元格中。 单元对象将在表视图滚动时被重新使用,因此您将丢失选中标记,并在不应该包含它们的单元格中得到选中标记。

您需要在另一个结构中跟踪已检查的单元格。 我建议使用Set<IndexPath> 您可以在表视图中允许多选,或者(我的偏好)在添加选中标记后取消选择该行。

您还需要确保您的cellForRowAt:正确设置附件类型

class CreateEventStep2VC: UIViewController {

    @IBOutlet weak var eventTypeNameLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!

    var newEvent : [String:Any]!
    var eventTypeAvailableData = [String]()
    var selectedEventTypes = Set<IndexPath>()

    override func viewDidLoad() {
        super.viewDidLoad()

        // initial value
        eventTypeNameLabel.text = ""

        // get event Type Data list from EventType data model
        eventTypeAvailableData = EventType.allValues.map { $0.toString() }
    }

}

extension CreateEventStep2VC : UITableViewDataSource {

    //MARK: - UITableViewDatasource

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return eventTypeAvailableData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "EventTypeCell", for: indexPath) as! CreateEventStep2Cell
        cell.eventTypeNames = eventTypeAvailableData[indexPath.row]
        cell.accessoryType = selectedEventTypes.contains(indexPath) ? .checkMark:.none

        return cell
    }

}


extension CreateEventStep2VC : UITableViewDelegate {

    //MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        if selectedEventTypes.contains(indexPath) {
            selectedEventTypes.remove(indexPath)
        } else if selectedEventTypes.count < 3 {
            selectedEventTypes.insert(indexPath)
        }
        tableView.reloadRows(at: [indexPath], animated:.none)
    }
}

你可以有indexPath行的阵列allArr这样

1-当用户选择3个以上时,第一个将自动删除

var allArr = [Int]()

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

     if let cell = tableView.cellForRow(at: indexPath) {
        cell.accessoryType = .checkmark
        allArr.append(indexPath.row)
     }

     if(allArr.count == 4)
     {
         allArr.dropFirst()

     }

}

2-将其添加到cellForRow

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "EventTypeCell", for: indexPath) as! CreateEventStep2Cell
    cell.eventTypeNames = eventTypeAvailableData[indexPath.row]

  if allArr.contains(indexPath.row)  {
       cell.accessoryType = .checkmark
   }
   else
   {
      cell.accessoryType = .none
   }
    return cell
}

3-删除didSelectRowAt中的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM