【问题标题】:using indexPath from table row in outside function swift在外部函数swift中使用表行中的indexPath
【发布时间】:2020-10-18 09:46:05
【问题描述】:

对于 swift 和 uni 项目的新手,但我有一个表格视图,其中包含朋友详细信息的记录

用户可以选择编辑朋友或删除朋友。 为此,我创建了一个长按手势来删除朋友,但我不确定如何将 indexPath 传递给函数

这是我目前的布局:

import UIKit

class view_19342665: UIViewController,UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    
    var friends: [Friends] = []
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return friends.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "friends", for: indexPath)
        cell.textLabel?.adjustsFontSizeToFitWidth = true;
        cell.textLabel?.font = UIFont.systemFont(ofSize: 13.0)
        cell.textLabel?.text = friends[indexPath.row].displayInfo
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let id = friends[indexPath.row].studentID
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.removeRecord(id: Int(id))
    }
    

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.delegate = self
        tableView.dataSource = self
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        friends = appDelegate.getFriendInfo()
        
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
        tableView.addGestureRecognizer(longPress)
        
        
        self.tableView.rowHeight = 33.0
        
    }
    override var canBecomeFirstResponder: Bool{
        return true
    }
    @objc func handleLongPress(sender:UILongPressGestureRecognizer ){
        if sender.state == .began{
            // delete user
            
        }
        else{
            //edit user
        }
        
    }
    
}

现在我可以删除表格函数中的行了。

这就是我想要达到的目标:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let id = friends[indexPath.row].studentID

        handleLongPress(id)
    }

func handleLongPress(id : Int){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
 if sender.state == .began{
            appDelegate.removeRecord(id: Int(id))
            }
else{
  appDelegate.editRecord(id: Int(id))


}

有人可以帮我在长按手势上使用 ID 删除一行

【问题讨论】:

  • 我不会使用长按手势;表格视图支持标准的向左滑动删除
  • 我还在学习 swift 的基础知识,这是一项复杂的任务吗?
  • @Paulw11 在示例代码中 object 对 object.remove 的引用是什么?我收到错误“找不到对象”
  • 在您的情况下,它将是您的 friends 数组。您还需要从任何持久存储中删除数据,例如 Core Data 或您正在使用的任何内容。另外,我不建议使用您的 AppDelegate 作为您的数据访问类。应该有一个模型类来处理它。

标签: ios swift xcode uitableview


【解决方案1】:

手势应该添加到cellForRowAt内的表格单元格中,而不是表格本身

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
cell.contentView.addGestureRecognizer(longPress)
cell.contentView.tag = indexPath.row

然后

@objc func handleLongPress(_ tap:UILongPressGestureRecognizer) {
   guard  tap.state == .ended && let index = tap.view?.tag else { return }
   // use index
}   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2014-01-30
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多