gpt4 book ai didi

uitableview - Swift - 检索存储在自定义 tableview-cells 中的信息

转载 作者:行者123 更新时间:2023-11-28 10:25:12 25 4
gpt4 key购买 nike

我已经在我的自定义单元格中实现了 longPressGesture,现在我需要检索存储在这些单元格中的信息。

我的 cellForRowAtIndexPath 函数如下所示:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

let longPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "cellLongPressed:")
longPress.delegate = self
longPress.minimumPressDuration = 1
longPress.numberOfTouchesRequired = 1

let cellID = "cell"
var mcell:CusCell = self.tv.dequeueReusableCellWithIdentifier("cell") as CusCell

mcell.addGestureRecognizer(longPress)

let data = mainList[indexPath.row] as SecondModel

var dateStr:String = String()
dateStr = printDate(data.date)

mcell.mainLabel.text = data.receiver
mcell.recLabel.text = "Message sent at \(dateStr)"
mcell.imageLabel.image = UIImage(named: icons[0])
mcell.messType = messageType

返回mcell

我的 didSelectRowAtIndexPath 函数如下所示:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let selectedCell: CusCell = tv.cellForRowAtIndexPath(indexPath) as CusCell

messagType = selectedCell.messType

println(messagType)

}

我的 cellLongPressed 函数如下所示:

func cellLongPressed(gestureRecognizer:UIGestureRecognizer) {

if (gestureRecognizer.state == UIGestureRecognizerState.Ended) {
println("STATE ENDED")
//Do Whatever You want on End of Gesture
}
else if (gestureRecognizer.state == UIGestureRecognizerState.Began){
println("STATE BEGAN")
//Do Whatever You want on Began of Gesture
}
}

现在您可能已经猜到了,存储在“selectedCell.messType”中的整数永远不会被打印出来。我真的不明白为什么这不起作用,是我对“selectedCell”的声明有误吗?

如有任何建议,我们将不胜感激。

最佳答案

如果 messageType 是模型的一部分,为什么不能从模型中读回数据?从单元读回数据并不是一个好的方法。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)  {

let data = mainList[indexPath.row] as SecondModel
var messagType = data.messageType
println(messagType)

}

如果您想记录长按事件中的值

func cellLongPressed(gestureRecognizer:UIGestureRecognizer) {

if (gestureRecognizer.state == UIGestureRecognizerState.Ended) {
var point = gestureRecognizer.locationInView(self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(point)
{
let data = mainList[indexPath.row] as SecondModel
var messagType = data.messageType
println(messagType)
}
}
else if (gestureRecognizer.state == UIGestureRecognizerState.Began){

}
}

关于uitableview - Swift - 检索存储在自定义 tableview-cells 中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27824219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com