【问题标题】:didDeselectRowAtIndexPath only called after pressing another cell [duplicate]didDeselectRowAtIndexPath 仅在按下另一个单元格后调用[重复]
【发布时间】:2015-05-16 15:50:32
【问题描述】:

事情是这样的:

我实现了一个带有自定义单元格的 UITableViewController.. 然后我用viewcontroller to viewcontroller替换了自动转场cell to viewcontroller(故事板)。

来自我的 UITableViewController 的代码:

// MARK: - Tableview managment

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var lobbycell = tableView.dequeueReusableCellWithIdentifier("lobbycell") as! LobbyTableViewCell
        let row = indexPath.row
        if let channel = lobbies[row].lobbyName {
            lobbycell.lobbynameLabel.text = channel
        }
        if let usercount = lobbies[row].users?.count {
            lobbycell.usercountLabel.text = "Anzahl Spieler: \(usercount)"
        }
        if let host = lobbies[row].host?.username {
            lobbycell.hostnameLabel.text = "Host: \(host)"
        }
        return lobbycell
    }

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

    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        let selectedLobby = lobbies[indexPath.row]
        var loading = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
        PFLobby.joinLobbyInBackgroundWithBlock(selectedLobby, user: PFUser.currentUser()!) { (success, error) -> Void in
            loading.hide(true)
            if success {
                self.performSegueWithIdentifier("joinLobby", sender: self)
            } else {

            }

        }
    }

现在问题来了: 如果我单击一个单元格,它将变为灰色(如选中)但不会调用 didDeselectRowAtIndexPathfunc.. 只有当我点击另一个单元格时,它才会调用 didDeselectRowAtIndexPath 带有灰色(如选中)单元格的 indexPath。

【问题讨论】:

  • 您的意思是/期望使用didSelectRowAtIndexPath 而不是didDeselectRowAtIndexPath? (选择与取消选择)
  • Omg xD 当然......对不起,自动完成谢谢......

标签: ios uitableview swift


【解决方案1】:

您是否对didSelectRowAtIndexPathdidDeselectRowAtIndexPath 感到困惑?

只有在取消选择时才会调用后者,因此在您选择另一个单元格后会调用它。

如果要调用didDeselectRowAtIndexPath,则在didSelectRowAtIndexPath中添加方法deselectRowAtIndexPath,它会取消选择正确的方式。

【讨论】:

    【解决方案2】:

    这是预期的行为。 tableView(_:didDeselectRowAtIndexPath:) 只会在选择另一个单元格后调用。

    如果你愿意,可以截取tableView(:_willSelectRowAtIndexPath:)中的选择事件:

    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    
        if let selectedIndex = tableView.indexPathForSelectedRow() where selectedIndex == indexPath {
            tableView.deselectRowAtIndexPath(indexPath, animated: true)
            return nil
        }
    
        return indexPath
    }
    

    将选择此方法返回的索引路径。如果您返回 nil,则不会选择任何内容。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2020-02-24
    • 2013-08-12
    • 1970-01-01
    • 2014-04-09
    相关资源
    最近更新 更多