【问题标题】:IOS Swift how can I highlight a cell on tap with TableView recyclingIOS Swift如何使用TableView回收突出显示一个单元格
【发布时间】:2016-12-14 18:46:58
【问题描述】:

我有一个 TableView 和一个名为 Post 的标签。我有一个 Gesture Tap 功能,当用户点击该标签时,被点击的 TableView 标签会改变颜色。问题在于 tableView 回收如果我走下 tableView 然后其他未点击的单元格也会突出显示。如何修复它以便仅突出显示单击的单元格?这是我的代码

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

        cell.post.text = Posts[indexPath.row]
        let post_tap = UITapGestureRecognizer(target: self, action: #selector(UserProfileC.post_tapped(sender:)))
        cell.post.addGestureRecognizer(post_tap)
        return cell
    }


    func post_tapped(sender:UITapGestureRecognizer) {

        let point = CGPoint(x: 0, y: 0)

        let position: CGPoint = sender.view!.convert(point, to: self.TableSource)
        if self.TableSource.indexPathForRow(at: position) != nil {
            sender.view?.backgroundColor = UIColor.blue

        }
    }

代码再次起作用,它突出显示了正确的 TableCell 标签,问题是当向下滚动 tableView 时,其他 tableCells 标签也被突出显示而没有被点击。

我已经用下面给出的示例更新了代码,但它仍然给出了相同的结果

if let existingRecognizerView = cell.viewWithTag(101) as UIView?  {

        existingRecognizerView.backgroundColor = UIColor.white


    } else {

let post_tap = UITapGestureRecognizer(target: self, action: #selector(UserProfileC.post_tapped(sender:)))
        post_tap.view?.tag = 101

    cell.post.addGestureRecognizer(post_tap)

    }

【问题讨论】:

    标签: ios swift uitableview swift3


    【解决方案1】:

    在您的cellForRowAtIndexPath 函数中,您正在使一个单元格出列,该单元格上已经有一个蓝色背景的 UITapGestureRecognizer。您需要在其视图中添加一个标签,以便在出队时可以访问它并删除背景颜色。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfileTVC", for: indexPath) as! UserProfileTVC
    
        cell.post.text = Posts[indexPath.row]
    
        // Check if we already have a GestureRecognizer view
        if let existingRecognizerView = cell.post.viewWithTag(101) {
            existingRecognizerView.backgroundColor = UIColor.white
        } else {
            let post_tap = UITapGestureRecognizer(target: self, action: #selector(UserProfileC.post_tapped(sender:)))
            cell.post.addGestureRecognizer(post_tap)
            post_tap.view.tag = 101
        }
    
        return cell
    }
    
    
    func post_tapped(sender:UITapGestureRecognizer) {
    
        let point = CGPoint(x: 0, y: 0)
    
        let position: CGPoint = sender.view!.convert(point, to: self.TableSource)
        if self.TableSource.indexPathForRow(at: position) != nil {
            sender.view?.backgroundColor = UIColor.blue
    
        }
    }
    

    *注意:从移动设备编码...未测试语法或功能错误。

    【讨论】:

    • 哦,好的,谢谢您的指导,我输入了您的代码,尽管我仍然得到相同的结果,但它确实感觉方向正确。我将发布我刚刚实施的内容。我从 as?到 UIView?和以前一样,我遇到了一个向下转换错误和相似之处,这个 post_tap.view?.tag 但其他一切都是一样的。
    • 好的,我刚刚做了一个更改。没有意识到您的单元格中有一个子视图。添加了if let existingRecognizerView = cell.post.viewWithTag(101)。您可以在existingRecognizerView.backgroundColor = UIColor.white 行上放一个断点,看看它是否被调用?
    • 完美,非常感谢,是的,我把它丢了断点,它被调用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2020-06-27
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多