Swift 如何将视图TableViewCell强制转换为UIGestureView
我正在尝试更改手势视图点击上的“我的表格视图”单元格内容,但出现了错误 线程1:信号SIGABRT 无法将“UILabel”(0x117f4ad68)类型的值强制转换为“MyApplication.Read”Swift 如何将视图TableViewCell强制转换为UIGestureView,swift,uitableview,swift3,uiview,uigesturerecognizer,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!
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
}
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)"
}
再次点击标签我想改变标签的内容,说你点击了单元格(CellIndex)。我可以在每次单击时获得正确的索引,但是在“Let Test”(让测试)中会出现错误。一些背景点:不要每次添加单元格时都添加手势识别器。随着你的细胞被回收,你将拥有越来越多的手势识别器。在自定义单元格类中添加一个标志,告诉您是否已经添加了手势识别器,如果尚未添加,则仅添加一个 (不会阻止代码工作,但仍然很重要)。Swift中的方法名称和变量名称应以小写字母开头。类和类型名称应以大写字母开头
至于你的强制施法失败的原因,听起来像是你牢房的
mycell.comment
出口不是你想要的类型Read
。根据错误,它的类型是UILabel
,而不是Read
(不管是什么)。但是,您没有为我们提供足够的信息来帮助处理该零件。您需要解释如何设置my class的出口,还需要解释什么是读取类型。您被添加到UILabel
<代码>注释
是UILabel
let Gesture = UITapGestureRecognizer(target: self, action: #selector(ReadAnswer(_:)))
Gesture.delegate = self as? UIGestureRecognizerDelegate
mycell.comment.addGestureRecognizer(Gesture)
在手势动作中,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)"
}
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
}
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, 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
}
我们必须用字典来解释这件事。因此,我们可以避免tableView重用。你可以给我的Uisteper。逻辑如下
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()
}
var textDict=[Int:String]()
重写func viewDidLoad(){
super.viewDidLoad()
对于0..Int中的i{
返回70
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“table”,for:indexPath)作为!TblTableViewCell
让手势=UITapGestureRecognitor(目标:自我,操作:#选择器(ReadAnswer))
signature.delegate=self-as?UIgestureRecognitizerDelegate
cell.commentLbl.tag=indexPath.row
cell.commentLbl.isUserInteractionEnabled=true
cell.commentLbl.AddGestureRecognitor(手势)
cell.commentLbl.text=textDict[indexPath.row]
返回单元
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
返回50
}
@objc func ReadAnswer(\发送方:UIgestureRecognitor){
让CellIndex:Int=(sender.view?.tag)!
textDict[CellIndex]=“您已单击单元格\(CellIndex)”
tblView.reloadData()
}
在您的ReadAnswer
函数中,sender
引用了它所附加到的视图。该视图是您命名的注释的UILabel
按如下方式更改您的功能:
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)"
}
}
这是我刚刚写的测试答案
我的阵列
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"]
我的tableview代表
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
}
点击手势处理程序
@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!
}
我的手机类
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时
单击TableView单元格时
你能添加30多个单元格并检查吗?是否重复使用?同样,你能解释更多吗?我最多可以添加30行,下一步要做什么?单击,第1行,更改标签文本,然后滚动到底部,看到任何差异吗?好的,我得到了重新确定的问题,解决了检查更新的答案,谢谢你的信息,我错过了做这个示例好的,请核对我的答案。两者都是相同的,