gpt4 book ai didi

ios - 滑动手势不改变 UITableViewCell

转载 作者:行者123 更新时间:2023-11-30 11:59:06 24 4
gpt4 key购买 nike

我试图用一堆单元格填充 uitableview,然后在滑动其中一个单元格时仅将单个单元格移动为由不同的 Nib 填充。当我刷手机时什么也没有发生。下面是代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//below registers the nib's for the contact cell
let nib = UINib(nibName: "ContactCell", bundle: nil)

self.myTableView.register(nib, forCellReuseIdentifier: "cell")
let cell = myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ContactCell
//below gets the index
var val = indexPath.row

//below assigns the values of the fields on the cell
cell.contactName.text = (name[val])
return cell

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)

return UITableViewCell()
}

下面是被调用的函数:

func handleGesture(gesture: UISwipeGestureRecognizer, indexNum: IndexPath) -> Void {
if gesture.direction == UISwipeGestureRecognizerDirection.right {
//sets the nib2 = to the cell for right swipe, which is then called in CellForRowAt
print("Swipe Right")
let nib2 = UINib(nibName: "RightOptionCell", bundle: nil)
self.myTableView.register(nib2, forCellReuseIdentifier: "cell2")
let cell2 = myTableView.dequeueReusableCell(withIdentifier: "cell2", for: indexNum) as! LeftOptionCell
}
else if gesture.direction == UISwipeGestureRecognizerDirection.left {
//display the leftoptioncell
print("Swipe Left")
let nib2 = UINib(nibName: "DropDownOptionsCell", bundle: nil)
self.myTableView.register(nib2, forCellReuseIdentifier: "cell2")
let cell2 = myTableView.dequeueReusableCell(withIdentifier: "cell2", for: indexNum) as! DropDownOptionsCell
}
}

滑动单元格时没有执行任何操作,任何关于原因的想法

最佳答案

第一个问题是您要将手势识别器添加到 View 中。这意味着它会检测到您 View 中的滑动,而不是 tableViewCells 上的滑动;这不一定是一个问题,只是你需要进行数学计算来找出哪个单元格被刷了。

第二个问题是你的处理程序的签名是错误的;处理程序可以不带任何参数,也可以带一个参数,并且必须是手势识别器;您那里有一个索引路径,但它不起作用。

一般来说,处理这种情况的最常见方法是将滑动手势识别器放入 tableViewCell 中,并在滑动发生时让单元格通知其委托(delegate)(应该是您的 View Controller )。然后,viewController 修改您的模型并使用动画调用 reloadCell,并触发 cellForRowAt,它将根据您更新的模型提供一个新单元格。

关于ios - 滑动手势不改变 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47463729/

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