当我从UITableViewSource单击RowSelected时如何打开UIViewController

时间:2017-08-27 06:50:32

标签: c# ios xamarin xamarin.ios

当我从UITableViewSource

中单击RowSelected时,我想要打开viewcontroller

我在打开UIViewController时使用此代码

非常感谢任何帮助。

感谢

MainStudUn controller =this.Storyboard.InstantiateViewController("MainStudUn") as MainStudUn; 
this.NavigationController.PushViewController(controller, true); 

但是在使用UITableViewSource

的代码时遇到了问题
class EmployeesTVS : UITableViewSource
{
    List<Employee> employees;

    public EmployeesTVS(List<Employee> employees)
    {
        this.employees = employees;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = (EmployeeCell) tableView.DequeueReusableCell("Cell_id", indexPath);
        var employee = employees[indexPath.Row];
        cell.updatecell(employee);
        return cell;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return employees.Count;
    }

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {

        var SelectName = employees[indexPath.Row];

        Globals.AskX = SelectName.Fullname;
        Globals.AnswerX = SelectName.Department;

        //OpnWerd();
        //MainStudUn controller = this.Storyboard.InstantiateViewController("MainStudUn") as MainStudUn;
        //this.NavigationController.PushViewController(controller, true);
    }
}

1 个答案:

答案 0 :(得分:1)

将ViewController本身设置为dataSource的初始化构造函数中的参数。

ViewController中的

this.TableView.Source = new EmployeesTVS(tableItems, this);

在dataSource

AskStud owner; //this is your VC class not ViewController,it's just a sample.
public EmployeesTVS(List<Employee> employees, AskStud  owner)
{
    this.employees = employees;
    this.owner = owner;
}

用法:

MainStudUn controller =this.owner.Storyboard.InstantiateViewController("MainStudUn") as MainStudUn; 
//OR 
//UIStoryboard Storyboard = UIStoryboard.FromName("StoryBoardName", null);
//MainStudUn controller = Storyboard.InstantiateViewController("MainStudUn") as MainStudUn;

this.owner.NavigationController.PushViewController(controller, true);