Eng.Ahmed
Eng.Ahmed

Reputation: 41

how to open UIViewController when i click RowSelected from UITableViewSource

i want open viewcontroller when i click RowSelected from UITableViewSource

i use this code when i open UIViewController

Any help would be greatly appreciated.

thanks

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

but i have issue when used this code from 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);
    }
}

Upvotes: 0

Views: 206

Answers (1)

ColeX
ColeX

Reputation: 14475

Set the ViewController itself as parameter in the initializing constructor of the dataSource.

in ViewController

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

in 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;
}

Usage:

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

Upvotes: 1

Related Questions