swift - UITableView 删除行

标签 swift tableview tableviewcell

我正在制作一个表格 View ,我想制作一个功能,您可以通过向右滑动并点击删除按钮来删除一行。我和我的老师已经尝试了大约半小时来解决这个问题,但似乎没有任何效果。

 import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var StoredValues = Values()
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {//
        super.didReceiveMemoryWarning()
    }

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "meunSegue", sender: self)

        func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            _ = segue.destination as! SecondViewController
        }
    }
    func tableView->(UITableView *)tableView canEditRowsAtIndexPath:(NSIndexPath *)indexPath {
    return YES
    }
    - (void)tableView:(UITableView *)tableView committEditStyle: (UITableViewCellEditingStyle)
    func tableView {
        var cell = UITableView.self
        var Animation = UITableViewRowAnimation(rawValue: 1)
        if editingStyle == UITableViewCellEditingStyle.delete {
            cell.deleteRows(indexPath)
            //cell.deleteRows(at: [NSIndexPath], with: UITableViewRowAnimation.automatic)

        }
    }


    class SecondViewController: UIViewController {

        var recievedData = ""
        override func viewDidLoad() {
            super.viewDidLoad()
            print(recievedData)
        }
    }
}

最佳答案

这是因为您的 numberOfRowsInSection 数据源实现始终返回 1(固定)。

通常,您将对象存储在一个数组中,该数组定义了行数。并且,commitEditingStyle 应该从数组中删除对象,然后删除行。

– (void)tableView: (UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {
       // Delete the row from the data source
       [maTheData removeObjectAtIndex:[indexPath row]];
       // Delete row using the cool literal version of [NSArray arrayWithObject:indexPath]

       [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }
}

关注这个link了解更多详情。

关于swift - UITableView 删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43744911/

相关文章:

ios - 在 IOS 中从 iPhone 中删除 Assets (图片或视频)

java - 如何根据 TableView 中的行元素自定义表格单元格/表格列内容(JavaFX)

ios - PrepareForSegue with UITableViewController - 传递数据

ios - 像在 Safari 中一样向 UISearchBar 添加 UIProgressView

ios - 当我在搜索栏中键入以检索 Google 图书信息时,我的表格 View 没有重新加载

ios - 删除 Tableview NSFetchedResultsController 中的最后一节行

ios - UITableVIew 的 tableViewCell 中看不到 Text Field 和 Labels

ios - 在 swift 2 的一个部分中的单元格之间添加空间

swift - 如何将我的 swift --version 同步到我的 xcrun swift --version?

ios - Swift:出现神秘约束