c# - 在 UITableView 单元格中选择 UIPIcker 索引

标签 c# ios swift xamarin xamarin.ios

我在单元格中创建了一个带有 UIPickerViewUITableView。我想在选择器上添加值并转到用户选择的项目。

UpdateCell 方法将值添加到选择器,AddSelection 方法选择选择器项目索引。

我遇到了超出范围的异常,因为 AddSelection 尝试选择索引,但 UpdateCell 尚未完成。这仅适用于 0 索引。

PreOrderMenuTableSource 类的内部...

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        // Product Title
        if (indexPath.Section == 0) {
            cellIdentifier = new NSString ("preOrderMenuInfoCell");
            var cell = tableView.DequeueReusableCell (cellIdentifier) as PreOrderMenuDescriptionCell;
            cell.UpdateCell (SelectedMenu);

            return cell;
        }

        // Product Description
        if (indexPath.Section == 1) {
            cellIdentifier = new NSString ("preOrderMenuDescCell");
            var cell = tableView.DequeueReusableCell (cellIdentifier) as PreOrderMenuTitleCell;
            cell.UpdateCell (SelectedMenu);

            return cell;
        }

        //Product Type Cell
        if (indexPath.Section == 2) {
            cellIdentifier = new NSString ("preOrderTypeCell");
            var cell = tableView.DequeueReusableCell (cellIdentifier) as PreOrderMenuTypeCell;
            cell.UpdateCell (SelectedMenu);
            cell.AddSelection (SelectedMenu);
            return cell;
        }

        // Comments
        if (indexPath.Section == 3) {
            cellIdentifier = new NSString ("preOrderMenuCommentCell");
            var cell = tableView.DequeueReusableCell (cellIdentifier) as PreOrderMenuCommentCell;
            cell.Setup (tableView, indexPath);

            return cell;
        }

        else {
            return null;
        }

    }

PreOrderMenuTypeCell 如下...

    partial class PreOrderMenuTypeCell : UITableViewCell
{
    public PreOrderMenuTypeCell (IntPtr handle) : base (handle)
    {
    }

    public void UpdateCell (MobilePreOrderMenuModel selectedProduct)
    {
        TypeTitle.Text = selectedProduct.Menu [0].Types[0].Name;
        var typePickerModel = new PreOrderTypePickerModel (selectedProduct.Menu [0].Types [0].Products);
        TypePicker.Model = typePickerModel;
    }


    public void AddSelection (MobilePreOrderMenuModel selectedProduct)
    {
        TypePicker.Select (0, SelectedOrDefault (selectedProduct.Menu [0].Types [0].Products), true);
    }

    private int SelectedOrDefault (List<MobilePreOrderProductDetails> products)
    {
        var selected = products.Where (x => x.IsSelected).FirstOrDefault ();
        if (selected != null) {
            return selected.Sequence.Value-1;
        } 
        else {
            var defaultSelected = products.OrderBy (x => x.Sequence).FirstOrDefault ();
            defaultSelected.IsSelected = true;
            return 0;
        }
    }

}

PreOrderTypePickerModel 如下...

public class PreOrderTypePickerModel : UIPickerViewModel
{
    static List<MobilePreOrderProductDetails> _viewModel;
    static int _selectedIndex;

    public PreOrderTypePickerModel (List<MobilePreOrderProductDetails> viewModel)
    {
        _viewModel = viewModel;
    }

    public static string SelectedProductName {
        get {
            return _viewModel [_selectedIndex].Name;
        }
    }

    public override void Selected (UIPickerView pickerView, nint row, nint component)
    {

        _selectedIndex = (int)row;

        foreach (var item in _viewModel) {
            item.IsSelected = false;
        }

        _viewModel[_selectedIndex].IsSelected = true;

    }

    public override nint GetRowsInComponent (UIPickerView pickerView, nint component)
    {
        return _viewModel.Count;
    }

    public override nint GetComponentCount (UIPickerView pickerView)
    {
        return 1;
    }


    public override UIView GetView (UIPickerView pickerView, nint row, nint component, UIView view)
    {
        var label = new UILabel ();
        label.Text = _viewModel [(int)row].Name;
        label.BackgroundColor = UIColor.Clear;
        label.TextAlignment = UITextAlignment.Left;
        label.Font = UIFont.FromName ("Helvetica", 14);
        label.TextColor = UIColor.DarkGray;
        return label;
    }
}

最佳答案

我将 AddSelection 方法更改为此

public void AddSelection (MobilePreOrderMenuModel selectedProduct)
{
    TypePicker.Select (SelectedOrDefault (selectedProduct.Menu [0].Types [0].Products),0, true);
}

我修复了 Select 上的参数,现在它可以正常工作了。

enter image description here

Select 的声明是

void UIPickerView.Select(nint row, nint component, bool animated);

关于c# - 在 UITableView 单元格中选择 UIPIcker 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56972022/

相关文章:

ios - OpenGL 中的离屏帧缓冲区

Swift 泛型数字类型和数学

C# 继承 : casting of parent object to child

c# - TempData 只能访问一次?

ios - 如何解决这个边界问题

ios - swift UICollectionView : didSelectItemAtIndexPath not responding

ios - 如果分配给属性的字符串太长, TableView 单元格不显示

c# - 有没有办法在运行时构建新类型?

c# - 传递对象的最佳实践

ios - 从 xcode 10.1 安装到 iOS 12+ 时,应用程序在启动屏幕上卡住