2014-11-14 48 views
2

我使用VisualStudio(Xamario.iOS)中的部件​​實施表格划動菜單。下面是我的方法「GetCell」使用SWTableViewCell的表格單元划動菜單

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; 
} 

我不明白在這些方法中的任何異常的代碼,但畢竟這些,我發現了異常在行UIApplication.Main(參數,空「的AppDelegate」 )在Main方法中。

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

如果我跳過行「SetRightUtilityButtons」,我得到的所有行沒有任何問題。但我想有滑動式的菜單。

+0

我把它解決了。 – 2014-11-14 15:34:20

+0

我有同樣的問題..你是如何解決它的?謝謝! – SimonM 2015-05-27 23:04:06

+0

我不知道我做了什麼。但是如果你能分享你的代碼,我可以解決你的問題。 – 2015-05-29 16:21:09

回答

3

在遇到上述相同的錯誤信息後,我鑽入異常調用堆棧,發現按鈕的顏色需要明確設置。 the original project on github的幫助程序代碼有幾種幫助創建按鈕的方法。我翻譯了sw_addUtilityButtonWithColor函數,現在它完美地工作。

我還使用MVVMCross所以我增加了以下輔助方法的SWTableViewCell一個子類,如下所示:

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; 
    } 
} 

我現在用它來創建在我MvxTableViewSourceGetOrCreateCellFor方法我的工具按鈕。

希望能夠幫助其他任何人遇到這種情況。並且非常感謝Nagaraj提供幫助,如果我沒有做到這一點,我會接受你的報價!

0

我一直在使用SWTableViewCell做過Xamarin.iOS與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(); 
     }); 
    } 
}