Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Why is my ViewCellRenderer applied to ALL view cells in iOS?

$
0
0

I made a custom iOS renderer for a ViewCell, but when I use it it is being applied in other normal ViewCells other than my custom cell. The other normal ViewCells are having their background's changed to what is being set in my renderer. I'm guessing it is because of the cell key that is being used in the DequeueReusableCell method that is called in the base ViewCellRenderer. The key being used is "Xamarin.ViewCell", and it should probably be my own cell's key, i.e. "My.SignatureCell".
I'm on Xamamarin.Forms v1.2.2-pre3

XAML Page

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:models="clr-namespace:PilotNavigator.ViewModels;assembly=PilotNavigator"
    xmlns:ctrl="clr-namespace:PilotNavigator.Controls;assembly=PilotNavigator"
    x:Class="PilotNavigator.Pages.EticketFormPage">   

  <StackLayout>
    <TableView Intent="Form" HasUnevenRows="true">
      <TableView.Root>      
            <TableSection Title="">

                <EntryCell Label="S/S:" Text="{Binding CallSign}" />

                <!-- This ViewCell will have my custom renderer applied to it! Not wanted! -->
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout HorizontalOptions="FillAndExpand" Orientation="Horizontal">
                            <Label Text="Date:"/>
                            <DatePicker />
                        </StackLayout>
                    </ViewCell.View>
                </ViewCell>

                <!-- Some more EntryCells -->

                <ctrl:SignatureCell ImageBytes="{Binding SignatureImageBytes}" Height="200">
                    <ctrl:SignatureCell.View>
                        <ContentView VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" HeightRequest="200"> 
                        </ContentView>
                    </ctrl:SignatureCell.View>
                </ctrl:SignatureCell>       
            </TableSection>                     
        </TableView.Root>
    </TableView>
  </StackLayout>
</ContentPage>

SignatureCell.cs

public class SignatureCell : ViewCell
{
    public static readonly BindableProperty ImageBytesProperty = BindableProperty.Create<SignatureCell, byte[]>(p => p.ImageBytes, default(byte[]));

    public byte[] ImageBytes
    {
        get { return (byte[])GetValue(ImageBytesProperty); }
        set { SetValue(ImageBytesProperty, value); }
    }
}

SignatureCellRenderer.cs (the iOS renderer)

public class SignatureCellRenderer : ViewCellRenderer
{

    private UIButton _addSignature;
    private UIButton _signature;
    private UIImage _image;

    public override UITableViewCell GetCell(Cell item, UITableView tv)
    {
        var signatureCell = (SignatureCell)item;

        var cell = base.GetCell(signatureCell, tv);

        if (cell != null)
        {
            _addSignature = new UIButton(); 
            _signature = new UIButton(); 

            var bgcolor = UIColor.LightGray;
            cell.BackgroundView = new UIView { BackgroundColor = bgcolor };

            _addSignature.SetImage(UIImage.FromFile("Resources/Images/iconsignature.png"), UIControlState.Normal);
            _addSignature.SetTitleColor(UIColor.FromRGB(0x33, 0x33, 0x33), UIControlState.Normal);
            _addSignature.BackgroundColor = bgcolor;
            _addSignature.Font = UIFont.SystemFontOfSize(17);
            _addSignature.ImageEdgeInsets = new UIEdgeInsets(0, -10, 0, 0);
            _addSignature.Frame = new RectangleF(PointF.Empty, cell.Frame.Size);
            _signature.Frame = new RectangleF(PointF.Empty, cell.Frame.Size);

            cell.AddSubviews(_signature, _addSignature);
            SetSignature(signatureCell.ImageBytes);                
        }

        return cell;           
    }


    private void SetSignature(byte[] imgBytes)
    {
        //Dispose the previous image if there was one
        if (_image != null)
        {
            _image.Dispose();
            _image = null;
        }

        if (imgBytes != null && imgBytes.Length > 0)
        {
            _image = imgBytes.ToUIImage();
            _signature.Hidden = false;
            _signature.SetBackgroundImage(_image, UIControlState.Normal);
            _signature.Enabled = true;
            _signature.Layer.CornerRadius = 7;
            _signature.ClipsToBounds = true;

            _addSignature.Hidden = true;
        }
        else
        {
            _signature.Hidden = true;
            _signature.SetBackgroundImage(null, UIControlState.Normal);

            _addSignature.Hidden = false;

            _addSignature.Enabled = true;

            var bgImage =
                UIImage.FromFile("Resources/Images/buttondark.png");

                // throws NullReferenceException
                //  .CreateResizableImage(new UIEdgeInsets(16, 17, 17, 17), ,UIImageResizingMode.Stretch); 

            _addSignature.SetBackgroundImage(bgImage, UIControlState.Normal);
            _addSignature.SetTitle("Add Signature", UIControlState.Normal);
        }
    }
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles