C# 在行上绑定第二个UITableView在同一UIViewController-ios Xamarin中单击第一个UITableView

C# 在行上绑定第二个UITableView在同一UIViewController-ios Xamarin中单击第一个UITableView,c#,ios,uitableview,xamarin,uiviewcontroller,C#,Ios,Uitableview,Xamarin,Uiviewcontroller,我在同一UIViewController中有两个UITableView。 tblView1和tblView2 两个表视图都动态地提供数据。 现在我的问题是:我想在tblView1中选择的row的基础上加载tblView2中的数据 例如,当我从tblView1中选择一个公司名称时,tblView2需要填充该公司生产的汽车 我有我的第一个表tblView1的rowIndex,但是我不能从tblView1的RowSelected方法绑定我的tblView2。 我需要帮助装订我的tblView2。我该怎

我在同一UIViewController中有两个UITableView。 tblView1和tblView2

两个表视图都动态地提供数据。 现在我的问题是:我想在tblView1中选择的row的基础上加载tblView2中的数据

例如,当我从tblView1中选择一个公司名称时,tblView2需要填充该公司生产的汽车

我有我的第一个表tblView1的rowIndex,但是我不能从tblView1的RowSelected方法绑定我的tblView2。 我需要帮助装订我的tblView2。我该怎么做


希望这有帮助。为了简单起见,在单个文件中创建了所有内容

public partial class ViewController : UIViewController
{
    public ViewController() : base("ViewController", null)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.

        var companySource = new CompanyTableSource();
        tblView1.Source = companySource;

        companySource.ItemSelected += OnCompanyItemSelected;
    }

    //this will handle the ItemSelected event
    //here you will retrieve the data for the second UITableView
    void OnCompanyItemSelected(object sender, Company e)
    {
        //you can do it this way
        tblView2.Source = new CarTableSource(e.Id);

        List<Car> cars = GetCarsByCompanyId(e.Id);

        //or this whay
        tblView2.Source = new CarTableSource(cars);
    }

    List<Car> GetCarsByCompanyId(int id)
    {
        //Your logic to get the data goes here
        throw new NotImplementedException();
    }
}

public class CompanyTableSource : UITableViewSource
{
    // You can use the whole object or just an integer (companyId)
    public event EventHandler<Company> ItemSelected;

    public IList<Company> Items { get; private set; } = new List<Company>();

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        var item = Items[indexPath.Row];

        ItemSelected?.Invoke(this, item);
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        //Do your implementation
        throw new NotImplementedException();
    }

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

public class CarTableSource : UITableViewSource
{

    public IList<Car> Items { get; private set; } = new List<Car>();

    public CarTableSource(int companyId)
    { 
        //Get your data based on the companyId
    }

    //Or pass in the data already retreived. (I preffer this method)
    public CarTableSource(IList<Car> cars)
    {
        Items = cars;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        //Do your implementation
        throw new NotImplementedException();
    }

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

在CompanyTableSource类中创建一个公共事件并从ViewController订阅它,然后在该数据源的RowSelected方法中引发该事件。在ViewController中,您将有一个事件处理程序,该处理程序将被通知,并且您将能够根据第一个tableView的选定行获取第二个tableView所需的信息。这里没有VS向您显示代码,希望这至少能给您一个想法。如果您能帮助我,我可以为您提供我的TeamViewer。@apineda,或者如果您能给我一个链接,指向某个例子?/以不同的方式对我起作用。我试图在事件中绑定UITableView,但这不起作用,然后我在事件中动态创建了UITableView,然后向他提供了源代码,结果成功了:谢谢,你能看看我关于Xamarin的另一个问题吗?
public partial class ViewController : UIViewController
{
    public ViewController() : base("ViewController", null)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.

        var companySource = new CompanyTableSource();
        tblView1.Source = companySource;

        companySource.ItemSelected += OnCompanyItemSelected;
    }

    //this will handle the ItemSelected event
    //here you will retrieve the data for the second UITableView
    void OnCompanyItemSelected(object sender, Company e)
    {
        //you can do it this way
        tblView2.Source = new CarTableSource(e.Id);

        List<Car> cars = GetCarsByCompanyId(e.Id);

        //or this whay
        tblView2.Source = new CarTableSource(cars);
    }

    List<Car> GetCarsByCompanyId(int id)
    {
        //Your logic to get the data goes here
        throw new NotImplementedException();
    }
}

public class CompanyTableSource : UITableViewSource
{
    // You can use the whole object or just an integer (companyId)
    public event EventHandler<Company> ItemSelected;

    public IList<Company> Items { get; private set; } = new List<Company>();

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        var item = Items[indexPath.Row];

        ItemSelected?.Invoke(this, item);
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        //Do your implementation
        throw new NotImplementedException();
    }

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

public class CarTableSource : UITableViewSource
{

    public IList<Car> Items { get; private set; } = new List<Car>();

    public CarTableSource(int companyId)
    { 
        //Get your data based on the companyId
    }

    //Or pass in the data already retreived. (I preffer this method)
    public CarTableSource(IList<Car> cars)
    {
        Items = cars;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        //Do your implementation
        throw new NotImplementedException();
    }

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