如何将视图TableViewCell转换为UIGestureView

时间:2018-02-16 01:28:31

标签: swift uitableview swift3 uiview uigesturerecognizer

我正在尝试在手势视图点击上更改我的表格视图单元格内容,但是我收到了错误

主题1:信号SIGABRT

无法转换类型' UILabel' (0x117f4ad68)到' MyApplication.Read'

func ReadAnswer(_ sender: UIGestureRecognizer)  {
    let CellIndex = sender.view?.tag
    print(CellIndex!)
    let test = sender.view as! Read!  // This produces error

    test?.comment.text = "You have clicked on cell \(CellIndex)"
}

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

    mycell.comment.tag = indexPath.row
    mycell.comment.text = streamsModel.Posts[indexPath.row]

    let Gesture = UITapGestureRecognizer(target: self, action: #selector(ReadAnswer(_:)))
    Gesture.delegate = self as? UIGestureRecognizerDelegate

    mycell.comment.addGestureRecognizer(Gesture)

    return cell
}

再次在标签上单击我想更改该标签的内容,表示您已单击单元格(CellIndex)。我可以在每次点击时获得正确的索引,但是让测试它会出错。

4 个答案:

答案 0 :(得分:1)

您已将Gesture添加到UILabel .. !! commentUILabel

let Gesture = UITapGestureRecognizer(target: self, action: #selector(ReadAnswer(_:)))
Gesture.delegate = self as? UIGestureRecognizerDelegate

mycell.comment.addGestureRecognizer(Gesture)

Gesture Action中,sender.view应为UILabel。因此发生了以下错误。

无法转换类型' UILabel' (0x117f4ad68)到' MyApplication.Read'

func ReadAnswer(_ sender: UIGestureRecognizer)  {
    let CellIndex = sender.view?.tag
    print(CellIndex!)


    let test = sender.view as! Read!  // This produces error

    test?.comment.text = "You have clicked on cell \(CellIndex)"

}

解决

我们必须使用,字典为此。因此,我们可以避免使用tableView。您可以为UIStepper获取我的answer。逻辑如下。

var textDict = [Int : String]()
override func viewDidLoad() {
    super.viewDidLoad()

    for i in 0..<70 // numberOfRows
    {
        textDict[i] = "Hello"
    }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 70
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "table", for: indexPath) as! TblTableViewCell

    let Gesture = UITapGestureRecognizer(target: self, action: #selector(ReadAnswer))
    Gesture.delegate = self as? UIGestureRecognizerDelegate

    cell.commentLbl.tag = indexPath.row
    cell.commentLbl.isUserInteractionEnabled = true
    cell.commentLbl.addGestureRecognizer(Gesture)

    cell.commentLbl.text = textDict[indexPath.row]

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 50
}

@objc func ReadAnswer(_ sender: UIGestureRecognizer)  {

    let CellIndex : Int = (sender.view?.tag)!

    textDict[CellIndex] = "You have clicked on cell \(CellIndex)"

    tblView.reloadData()

}

答案 1 :(得分:0)

一些背景要点:每次添加单元格时都不要添加手势识别器。随着您的细胞被回收,您将获得越来越多的手势识别器。在自定义单元格类中添加一个标记,告知您是否已经添加了手势识别器,并且只有在您尚未完成此操作时才添加一个。

(不会阻止您的代码工作,但仍然很重要)。 Swift中的方法名称和变量名称应以小写字母开头。类和类型名称应以大写字母开头。

至于为什么你的强制转换失败,听起来你的单元格的mycell.comment出口不是所需类型Read。根据错误,它是UILabel类型,而不是Read(无论是什么。)但是,您还没有给我们足够的信息来帮助该部分。您需要解释如何设置我的课程,并解释Read类型的内容。

答案 2 :(得分:0)

ReadAnswer函数中,sender引用了它所附加的视图。该视图是您UILabel命名的comment

按如下方式更改您的功能:

func ReadAnswer(_ sender: UIGestureRecognizer)  {
    let CellIndex = sender.view?.tag
    print(CellIndex!)
    if let test = sender.view as? UILabel {

        test.comment.text = "You have clicked on cell \(CellIndex)"
    }
}

答案 3 :(得分:0)

这里是我刚刚写的测试答案

我的阵列

var staticArrayy = ["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"]

my tableview Delegate

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

    //Setting cells data
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = self.myDemoTableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomTableViewCell

        ///Adding Gesture
        let Gesture : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tableViewVC.ReadAnswer(_:)))
        Gesture.numberOfTapsRequired = 1
        Gesture.cancelsTouchesInView = false
        Gesture.delegate = self
        cell.comment.addGestureRecognizer(Gesture)

        cell.comment.isUserInteractionEnabled = true

        ///Assuming mycell.comment.text is a Label Connected
        cell.comment.text = staticArrayy[indexPath.row]

        return cell
    }

//Setting height of cells
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{        
    return 60
}

TapGesture处理程序

@objc func ReadAnswer(_ sender: UITapGestureRecognizer)
    {
        //Get position in View
        let myPosition = sender.location(in: self.myDemoTableView)

        //Get current Index Path
        let indexPath : IndexPath = self.myDemoTableView.indexPathForRow(at: myPosition)!

        //Time to Show Answer
        var cell = self.myDemoTableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomTableViewCell
        cell = self.myDemoTableView.cellForRow(at: indexPath) as! CustomTableViewCell
        cell.comment.text = "You have clicked on cell \(indexPath.row)"

        //Re-Deque issue
        //Update the Array Content at same Index whose value is changed
        self.staticArrayy[indexPath.row] = cell.comment.text!
    }

我的手机CLass

import UIKit

class CustomTableViewCell: UITableViewCell
{

    @IBOutlet weak var comment: UILabel!


    override func awakeFromNib()
    {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

输出:

第一次加载tableView时

enter image description here

单击TableView单元格时

enter image description here