【问题标题】:how to open UIViewController when i click RowSelected from UITableViewSource当我从 UITableViewSource 单击 RowSelected 时如何打开 UIViewController
【发布时间】:2017-08-27 06:50:32
【问题描述】:

当我从 UITableViewSource 单击 RowSelected 时,我想打开视图控制器

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

【问题讨论】:

    标签: c# ios xamarin xamarin.ios


    【解决方案1】:

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

    在视图控制器中

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

    【讨论】:

    • 错误 CS1061 'EmployeesTVS' 不包含对 'Storyboard' 的定义,并且找不到接受类型为 'EmployeesTVS' 的第一个参数的扩展方法 'Storyboard'(您是否缺少 using 指令或程序集参考?)
    • listVStud.Source = newEmployeesTVS(employees, this); listVStud.RowHeight = UITableView.AutomaticDimension; listVStud.EstimatedRowHeight = 100F; listVStud.ReloadData();
    • 错误 CS1503 参数 2:无法从“TrainingIOS.AskStud”转换为“TrainingIOS.ViewController”TrainingIOS D:\TrainingIOS\TrainingIOS\AskStud.cs 67
    • @Eng.Ahmed e......ViewController 是第一个 VC 类......查看我的更新答案......
    • 抱歉问题在这里:listVStud.Source = new EmployeesTVS(employees, this);无法从“TrainingIOS.AskStud”转换为“TrainingIOS.ViewController”
    猜你喜欢
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 2018-01-01
    • 2012-06-13
    • 2017-05-05
    • 2019-07-01
    相关资源
    最近更新 更多