添加Xamarin.iOS查看单元格背景颜色和角半径

时间:2019-02-20 02:17:13

标签: xamarin xamarin.ios

我正在尝试在所选行上添加角半径单元。我可以为所有未选中的人做到这一点。

这部分代码不起作用。

[assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
namespace PawsApp.iOS.Renderers
{
    public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
    {
        private UIView bgView;

        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            UITableViewCell viewCell = base.GetCell(item, reusableCell, tv);
            if (viewCell != null)
            {
                if (bgView == null)
                {
                    bgView = new UIView(viewCell.SelectedBackgroundView.Bounds);
                    bgView.Layer.BackgroundColor = Color.FromHex("#666495").ToCGColor();
                    bgView.Layer.CornerRadius = new nfloat(17.5);
                }

                viewCell.SelectedBackgroundView = bgView;            
            }
            return viewCell;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

[程序集:ExportRenderer(typeof(ViewCell),typeof(ViewCellItemSelectedCustomRenderer))]

您可以在表单中添加ViewCell的子类并创建其自定义渲染器。如

public class CustomVeggieCell : ViewCell
{
    public CustomVeggieCell()
    {
        //instantiate each of our views
        var image = new Image();
        var nameLabel = new Label();
        var typeLabel = new Label();
        var verticaLayout = new StackLayout();
        var horizontalLayout = new StackLayout() { BackgroundColor = Color.Olive };

        //set bindings
        nameLabel.SetBinding(Label.TextProperty, new Binding("Name"));
        typeLabel.SetBinding(Label.TextProperty, new Binding("Type"));
        image.SetBinding(Image.SourceProperty, new Binding("Image"));

        //Set properties for desired design
        horizontalLayout.Orientation = StackOrientation.Horizontal;
        horizontalLayout.HorizontalOptions = LayoutOptions.Fill;
        image.HorizontalOptions = LayoutOptions.End;
        nameLabel.FontSize = 24;

        //add views to the view hierarchy
        verticaLayout.Children.Add(nameLabel);
        verticaLayout.Children.Add(typeLabel);
        horizontalLayout.Children.Add(verticaLayout);
        horizontalLayout.Children.Add(image);

        // add to parent view
        View = horizontalLayout;
    }
}

我在github上共享了sample。您可以下载它进行测试。