How to make my Table Row Editable?

2019-08-23 05:41发布

问题:

may I know how to make the rows of my table can be swipe to delete or pop out a "red stop" button to allow user to delete the inserted data?

Below attached with the code of my Table ViewController cs file that only displays all data in table list :

*The view controller is created using storyboard, not by code.

using Foundation;
using System;
using UIKit;
using SQLite;
using System.Collections.Generic;
using System.IO;

namespace One
{
public partial class StudentDataBaViewController : UITableViewController
{
    private string pathToDatabase;
    private List<Student> students;

    public StudentDataBaViewController (IntPtr handle) : base (handle)
    {
        students = new List<Student>();
    }

    //connect to student_da.db database file and create a table named Student
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        //path of the database
        var documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        pathToDatabase = Path.Combine(documentsFolder, "student_db.db");

        //connect database and create table
        using (var connection = new SQLite.SQLiteConnection(pathToDatabase))
        {
            connection.CreateTable<Student>();
        }
     }

    //used to relaod or update new elements entered on the list
    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);

        students = new List<Student>();

        using (var connection = new SQLite.SQLiteConnection(pathToDatabase))
        {
            var query = connection.Table<Student>();

            foreach (Student student in query)
            {
                students.Add(student);
                TableView.ReloadData();

            }
        }
    }

    public override nint RowsInSection(UITableView tableView, nint section)
    {
        return students.Count;
    }

    //make elements to be display on the database list
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell("student");
        var data = students[indexPath.Row];

        cell.TextLabel.Text = data.StudentName;
        cell.DetailTextLabel.Text = data.StudentId;

        return cell;
    }

    public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
    {
        return true;
    }
}

public class Student
{
    [PrimaryKey]
    public string StudentId
    {
        get;
        set;
    }

    public string StudentName
    {
        get;
        set;
    }

    public string StudentPassword
    {
        get;
        set;
    }

    public bool StudentAttendence
    {
        get;
        set;
    }
}

}

回答1:

Add the following methods

public override void CommitEditingStyle (UITableView tableView,UITableViewCellEditingStyle editingStyle, Foundation.NSIndexPath indexPath){
switch (editingStyle) {
    case UITableViewCellEditingStyle.Delete:
        // remove the item from the underlying data source
        tableItems.RemoveAt(indexPath.Row);
        // delete the row from the table
        tableView.DeleteRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);
        break;
    case UITableViewCellEditingStyle.None:
        Console.WriteLine ("CommitEditingStyle:None called");
        break;
}}


回答2:

In addition to overriding the getcell method , you need to provide CommitEditingStyle

 public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        switch (editingStyle) {
        case UITableViewCellEditingStyle.Delete:
            // remove the item from the underlying data source
            tableItems.RemoveAt(indexPath.Row);
            // delete the row from the table
            tableView.DeleteRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);
            break;
        default:
            break;
        }
    }