UITableView 删除行 [英] UITableView Delete Row

查看:50
本文介绍了UITableView 删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a table view and I want to make a function where you can delete a row by swiping right and tapping the delete button. Me and my teacher have tried for about half an hour to fix this problem but nothing seems to work.

 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(固定).

This is because your numberOfRowsInSection data source implementation always returns 1 (fixed).

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

Typically, you store objects in an array, which defines the number of rows. And, commitEditingStyle should remove the object from the array and then delete the row.

– (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];

    }
}

按照此链接了解更多详情.

这篇关于UITableView 删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆