2015-04-07 37 views
0

我正在使用UITableView在Xamarin中创建菜单。我正在寻找最好的(最干净的)方法来将操作绑定到按钮单击/ RowSelected事件。目前,我正在使用以下方法来检查哪个按钮被点击。但是,由于我经常检查菜单项名称,所以这看起来有点脏。达到同一目标必须有更好的方式。任何帮助或提示正确的方向将不胜感激。Xamarin UITableView创建菜单

我TableViewSource:

public class MenuSource : UITableViewSource 
{ 
    private List<string> categories; 
    private List<string> firstMenuItems; 
    private List<string> secondMenuItems; 
    private List<string> thirdMenuItems; 
    private UITableViewController parent; 

    public MenuSource (UITableViewController parentController) 
    { 
     this.categories = new List<string>() {"First menu category", "Second menu category", "Third menu category" }; 
     this.firstMenuItems = new List<string>() {"First", "Second", "Third" }; 
     this.secondMenuItems = new List<string>() { "First" }; 
     this.thirdMenuItems = new List<string>() {"First", "Second" }; 
     this.parent = parentController; 
    } 

    public override nint NumberOfSections (UITableView tableView) 
    { 
     return categories.Count; 
    } 

    public override string TitleForHeader (UITableView tableView, nint section) 
    { 
     return categories [(int)section]; 
    } 

    public override nint RowsInSection (UITableView tableview, nint section) 
    { 
     if (categories [(int)section] == "First menu category") { 
      return firstMenuItems.Count; 
     } else if (categories [(int)section] == "Second menu category") { 
      return secondMenuItems.Count; 
     } else { 
      return thirdMenuItems.Count; 
     } 
    } 

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) 
    { 
     UITableViewCell cell = tableView.DequeueReusableCell ("MenuCell"); 

     if(cell == null){ 
      cell = new UITableViewCell (UITableViewCellStyle.Default, "MenuCell"); 
     } 

     if (categories [(int)indexPath.Section] == "First menu category") { 
      cell.TextLabel.Text = firstMenuItems [indexPath.Row]; 
     } else if (categories [(int)indexPath.Section] == "Second menu category") { 
      cell.TextLabel.Text = secondMenuItems [indexPath.Row]; 
     } else { 
      cell.TextLabel.Text = thirdMenuItems [indexPath.Row]; 
     } 

     return cell; 
    } 

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
    { 
     if (categories [(int)indexPath.Section] == "First menu category") { 
      if (firstMenuItems [(int)indexPath.Row] == "First") { 

      } else if (firstMenuItems [(int)indexPath.Row] == "Second") { 

      } else { 

      } 
     } else if()... 

      etc. 

    } 

回答

0

我不能完全肯定,当你说菜单是什么意思?假设这只是一个简单的UITableView,那么你是否考虑使用MvvmCross?

然后,你可以做结合,像这样:

set.Bind (MyTable).For ("SetDataSource").To ((vm) => (vm.SavedItems)).WithConversion (tableadapter,null); 

var tableadapter = new BackupRestoreTableCellConverter (MyTable, DataContext); 

看一看斯图尔特·洛奇的视频N = 3 & N = 6.5 here他的影片是伟大的!

希望这会有帮助