2
votes

I'm implementing the Table Swipe menu using the component SWTableViewCell, in VisualStudio (Xamario.iOS). Below is my code for the method "GetCell"

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)`<br/>
{
    SWTableViewCell.SWTableViewCell cell = null;
    cell =  (SWTableViewCell.SWTableViewCell)tableView.DequeueReusableCell(cellIdentifier);

    if (cell == null)
        cell = new SWTableViewCell.SWTableViewCell(UITableViewCellStyle.Subtitle, cellIdentifier);

    cell.SetRightUtilityButtons(GetButtons(), 30.5f);
    cell.Delegate = new SWTableViewCell.SWTableViewCellDelegate();

    cell.TextLabel.Text = tableItems[indexPath.Row];
}


private UIButton[] GetButtons()
{
    UIButton[] buttons = new UIButton[1];

    UIButton btn = new UIButton();
    btn.SetTitle("Delete", UIControlState.Normal);

    buttons[0] = btn;
    UIImage image = UIImage.FromFile("Profile/Images/house");
    btn.SetImage(image, UIControlState.Normal);

    return buttons;
}

I do not get any exception in these methods but after all these I'm getting the exception at the line UIApplication.Main(args, null, "AppDelegate") in Main method.

NSInvalidArgumentException Reason: *** -[__NSArrayM insertObject:atIndex:]: object cannot be nil

If I skip the line "SetRightUtilityButtons", I'm getting all the rows without any problem. But I would like to have slider kind of menu.

2
I'm having the same issue.. How did you get it resolved? Thanks! - SimonM
I'm not sure what I did. But I can fix yours if you can share your code. - Nagaraj .

2 Answers

3
votes

Having ran into the same error message as above, I dug into the exception call stack and found that the color of the button needs to be set explicitly. The helper code from the original project on github has several methods that help create buttons. I translated the sw_addUtilityButtonWithColor function and it now works perfectly.

I'm also using MVVMCross so I added the following helper method to a subclass of SWTableViewCell as follows:

public class MvxSwTableViewCell
    : SWTableViewCell, IMvxBindable
{

    // Rest of class omitted

    public static UIButton GetUtilityButtonWithColor(UIColor color, string title)
    {
        var button = new UIButton(UIButtonType.Custom);
        button.BackgroundColor = color;
        button.SetTitle(title, UIControlState.Normal);
        button.SetTitleColor(UIColor.White, UIControlState.Normal);
        button.TitleLabel.AdjustsFontSizeToFitWidth = true;

        return button;
    }
}

I now use this to create my utility buttons in the GetOrCreateCellFor method in my MvxTableViewSource.

Hope that helps anyone else that runs into this. And a big thank you to Nagaraj for offering to help, if I hadn't worked this out I would've taken up your offer!

0
votes

I have done using SWTableViewCell in Xamarin.iOS with Mvvmcross DataConsumer:

using System;
using Foundation;
using MvvmCross.Binding.BindingContext;
using MvvmCross.Binding.iOS.Views;
using SWTableViewCells;
using UIKit;

namespace SubViews
{
    public partial class SampleTableViewCell : SWTableViewCell, IMvxBindable
    {
    public static readonly NSString Key = new NSString ("SampleTableViewCell");
    public static readonly UINib Nib;

    public IMvxBindingContext BindingContext { get; set; }

    public object DataContext 
    {
        get { return BindingContext.DataContext; }
        set { BindingContext.DataContext = value; }
    }

    static SampleTableViewCell ()
    {
        Nib = UINib.FromName ("SampleTableViewCell", NSBundle.MainBundle);
    }

    public static SampleTableViewCell Create ()
    {
        return (SampleTableViewCell)Nib.Instantiate (null, null) [0];
    }

    public SampleTableViewCell (string bindingText, IntPtr handle)
        : base(handle)
    {
        this.CreateBindingContext (bindingText);
    }

    protected SampleTableViewCell (IntPtr handle) : this (string.Empty, handle)
    {
        this.DelayBind (() => {
            var bindings = this.CreateBindingSet<SampleTableViewCell, Type> ();
            //Custom Bindings
            bindings.Apply ();
        });
    }
}