gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:12:56 24 4
gpt4 key购买 nike

当我从 UITableViewSource 中单击 RowSelected 时,我想打开 View Controller

我在打开 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);
}
}

最佳答案

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

在 View Controller 中

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

在数据源中

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);

关于c# - 当我从 UITableViewSource 单击 RowSelected 时如何打开 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com