【问题标题】:Table Cell Not Swiping表格单元格不滑动
【发布时间】:2021-09-18 00:02:35
【问题描述】:

我正在开发一个与Realm 数据库链接的待办事项列表应用程序,但是当我尝试使用允许用户在单元格上滑动以删除数据的“editingStyle”方法时UI 和 Realm 数据库单元格不滑动,应用程序有 2 个屏幕,此方法在第一个屏幕上工作正常,但在另一个屏幕上不起作用,单元格工作正常,只是不会滑动。

我的代码:

    
import UIKit
import RealmSwift

class CategoryViewController: UITableViewController {
    
    
    var categories: Results<Category>?
    let realm = try! Realm()
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadCategories()
        
      tableView.rowHeight = 60.0

    }
    
    //MARK: - Creating the table view cell

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return categories?.count ?? 0
        
    }
    

    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        

   let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath)
        cell.textLabel?.text = categories?[indexPath.row].name ?? "No Categories Added Yet"

                return cell
        
    }
    
    //MARK: - This will remove a category from the UI & the Realm database, this is a built in swift method
    
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            if let  deleteAction  = categories?[indexPath.row] {
                do {
                    try realm.write({
                        realm.delete(deleteAction)
                    })
                } catch {
                print("Error deleting the cell \(error)")
                }
            }
        }
        
        tableView.deleteRows(at: [indexPath], with: .fade)
    }


    
    //MARK: - TableView Delegate Methods
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "goToItems", sender: self)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as! TodoListViewController
        
        if let indexPath = tableView.indexPathForSelectedRow {
            destinationVC.selectedCategory = categories?[indexPath.row]
        }
    }
    
    
    
    //MARK: - Add New Categories

    @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
        
        var textField = UITextField()
        
        let alert = UIAlertController(title: "Add New Category", message: "", preferredStyle: .alert)
        
        let action = UIAlertAction(title: "Add", style: .default) { (action) in
            // What happens when user clicks add button

            
            let newCategory = Category()
            newCategory.name = textField.text!

            
            
            self.saveCategories(category: newCategory)
            
        }
        
        alert.addAction(action)
        
        alert.addTextField { (field) in
            textField = field
            textField.placeholder = "Add a new category"
        }
        
        present(alert, animated: true, completion: nil)
        
    }
    

    //MARK: - Data Manipulation Methods
    
    func saveCategories(category: Category) {
        do {
            try realm.write({
                realm.add(category)
            })
        } catch {
            print("Error saving category \(error)")
        }
        
        tableView.reloadData()
        
    }
    
    func loadCategories() {
         categories = realm.objects(Category.self)
        
        tableView.reloadData()
  }


}



【问题讨论】:

  • 不刷卡或给出错误,因为您的代码看起来不错
  • 看看实施func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -&gt; Bool 能否解决您的问题

标签: ios swift uitableview swipe


【解决方案1】:

添加这个:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

【讨论】:

  • 试过了,没用,代码在我的其他tableViewCell中运行正常,可能是模拟器的问题
【解决方案2】:

您的代码似乎正确。我认为realm.delete(_:) 方法有问题,它可能会引发执行catch 块而不是删除行的错误。尝试使用一些打印语句查看try 块内是否有问题。如果一切都失败了,请尝试进行以下更改

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            if let  deleteAction  = categories?[indexPath.row] {
                do {
                    try realm.write({
                        realm.delete(deleteAction)
                    })
                    // New snippet
                    tableView.deleteRows(at: indexPath, with: .fade) 
                    self.tableView.reloadData()
                } catch {
                print("Error deleting the cell \(error)")
                }
            }
        }
        
    }

【讨论】:

  • 嘿,我不认为它与 do & catch 语句有关,当我在其中打印某些内容时,调试控制台不会打印任何内容,您建议我所做的更改但它给了我一个错误('UITableView' 类型的值没有成员'remove')这个确切的代码在应用程序的另一部分对我有用,所以我在这里有点困惑!
  • 如果不打印,说明print(_:) 语句之前的代码有问题。您能否重新打开 XCode,在您的函数调用中添加一些 print 语句,例如 ViewDidLoad(), do { try } catch。我会编辑帖子
  • 我找到了一个与您的问题类似的线程,看看那里的解决方案是否相同。 thread
  • 谢谢你,我修复了错误,这是我的 main.storyboard 中的一个问题,我的代码最后没问题!感谢帮助
猜你喜欢
  • 2016-02-19
  • 2014-03-21
  • 2014-01-16
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
相关资源
最近更新 更多