【问题标题】:allowsMultipleSelection is not working in Swift?allowMultipleSelection 在 Swift 中不起作用?
【发布时间】:2015-02-18 01:40:11
【问题描述】:

我很沮丧,因为它浪费了我很多时间。 我不知道为什么allowsMultipleSelection 在我的TableView 中不起作用? 必须做的所有事情才能使它工作,我做了,但仍然没有结果。 请查看我的代码,并告诉我问题所在。

override func viewDidLoad()
    {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.participantsTableView.allowsMultipleSelection = true
}

if tableView == participantsTableView
        {
            var cell = tableView.cellForRowAtIndexPath(indexPath)!

            if cell.accessoryType == UITableViewCellAccessoryType.Checkmark
            {
                cell.accessoryType = UITableViewCellAccessoryType.None
            }

            else
            {
                cell.accessoryType == UITableViewCellAccessoryType.Checkmark
            }

            self.participantsTableView.reloadData()
        }

【问题讨论】:

  • 你把代码if tableView == participantsTableView~放在哪里?在函数tableView:didSelectRowAtIndexPath:?如果是这样,您不需要重新加载表。
  • 在“didSelectRowAtIndexPath”中是的,我也尝试了同样的方法,但没有重新加载 tableView!
  • 请把代码贴在表格的数据源方法tableView:cellForRowAtIndexPath:
  • var cell = UITableViewCell() cell = tableView.dequeueReusableCellWithIdentifier("participantsCell") as UITableViewCell cell.textLabel.text = "test" return cell

标签: uitableview swift ios8 multipleselection


【解决方案1】:

您需要实现委托方法tableView:didDeselectRowAtIndexPath: 并更新tableView:cellForRowAtIndexPath: 中的cell 的accessoryType。

像这样:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        cell.accessoryType = .Checkmark
    }
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        cell.accessoryType = .None
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cellId", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel?.text = String(indexPath.row)

    if let selectedPaths = tableView.indexPathsForSelectedRows() as? [NSIndexPath] {
        let selected = selectedPaths.filter(){ $0 == indexPath }
        if selected.count > 0 {
            cell.accessoryType = .Checkmark
        } else {
            cell.accessoryType = .None
        }
    }
    return cell
}

【讨论】:

    猜你喜欢
    • 2020-07-11
    • 2014-08-05
    • 2014-07-27
    • 2014-07-29
    • 2016-02-18
    • 2015-03-19
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多