首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在表视图上显示Checkmark?

如何在表视图上显示Checkmark?
EN

Stack Overflow用户
提问于 2018-08-15 05:55:33
回答 1查看 58关注 0票数 0

我想做一个验证按钮/关闭按钮,以显示是学生验证。在"AddStudentViewController“中打开开关后,将在"StudentDataBaViewController”中的表列表中的特定学生行的右侧显示一个复选标记。我能知道怎么做吗?

**补充资料:

1)StudentDataBaViewContoller (应用程序的第一页)是一个TableViewController 2)AddStudentViewController (应用程序的第二页)是一个普通的视图控制器

两者都与名为"StuSegue“的Push Segue连接。

下面附上了我的2视图控制器的代码:

“StudentDataBaViewController”代码:

代码语言:javascript
复制
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>();
    }

    /*public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
    {
        if (segue.Identifier == "StuSegue")
        { // set in Storyboard
            var navctlr = segue.DestinationViewController as AddStudentViewController;
            if (navctlr != null)
            {
                var source = TableView.Source as StudentDataBaViewController;
                var rowPath = TableView.IndexPathForSelectedRow;
                var item = source.GetItem(rowPath.Row);
                navctlr.SetTask(this, item); // to be defined on the TaskDetailViewController
            }
        }
    } 
    */

    //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 override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, Foundation.NSIndexPath indexPath)
    {
        switch (editingStyle)
        {
            case UITableViewCellEditingStyle.Delete:
                // remove the item from the underlying data source
                students.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;
        }
    }

}

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

    public string StudentName
    {
        get;
        set;
    }

    public string StudentPassword
    {
        get;
        set;
    }

    public bool StudentAttendence
    {
        get;
        set;
    }
}
}  

“AddStudentViewController”代码:

代码语言:javascript
复制
using Foundation;
using System;
using System.IO;
using UIKit;

namespace One
{
public partial class AddStudentViewController : UIViewController
{
    private string pathToDatabase;


    //used to access same database to grab data for display
    protected AddStudentViewController (IntPtr handle) : base (handle)
    {
        // Note: this .ctor should not contain any initialization logic.
        var documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        pathToDatabase = Path.Combine(documentsFolder, "student_db.db");
    }

    // create a function for save button
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.
        saveStudent.Clicked += SaveButton_Clicked;
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }

    //create connection with database and make handler for save button and insert the element into database
    void SaveButton_Clicked(object sender, EventArgs e)
    {
        using (var connection = new SQLite.SQLiteConnection(pathToDatabase))
        {
            connection.Insert(new Student() { StudentName = AddStuNameTxt.Text, StudentId = AddStuIdTxt.Text, StudentPassword = AddStuPassTxt.Text, StudentAttendence =true  });
            new UIAlertView("Updated !", "Student successfully created", null, "OK", null).Show();
        }

        //navigate page back to database list
        NavigationController.PopToRootViewController(true);
    }


}
}
EN

回答 1

Stack Overflow用户

发布于 2018-08-15 06:13:41

根据我的理解,您希望在打开"StudentDataBaViewController“中的"AddStudentViewController”之后,在“AddStudentViewController”中刷新UI。您可以使用委托在两个控制器之间发送消息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51853597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档