如何使用“全选”和“取消全选”按钮获取UITableView

时间:2011-10-26 13:52:12

标签: mono uikit xamarin.ios uitableview

我想像selectall或deselectall这样的tableview配件按钮 cell.Accessory = UITableViewCellAccessory.None; 我想要一个按钮示例:“全选” 当用户触摸此按钮时,每个单元的附件都应该勾选。 或者我想要“重置”按钮。如果用户触摸此按钮,则每个复选标记都会消失,而Cell的附件也不会出现。

2 个答案:

答案 0 :(得分:5)

您可能已经发现使用UITableView有点复杂。然而,MonoTouch有一个很棒的库,名为MonoTouch.Dialog,可以让事情变得更容易。

以下示例代码使用MonoTouch.Dialog来回答您的问题(根据我的理解,如果我的答案与您想要的不符,请告诉我。)

    UIBarButtonItem [] selection_buttons;

    void Process (IList<Element> list, bool value)
    {
        foreach (Element e in list) {
            CheckboxElement cb = e as CheckboxElement;
            if (cb == null)
                continue;
            cb.Value = value;
            cb.GetImmediateRootElement ().Reload (cb, UITableViewRowAnimation.None);
        }
    }

    void Test ()
    {
        Section s = new Section ("Select items");
        for (int i = 0; i < 10; i++)
            s.Add (new CheckboxElement (i.ToString ()));
        var root = new RootElement (String.Empty);
        root.Add (s);

        var dv = new DialogViewController (root, true);

        // keep buttons in a field, not a local variable, to ensure it won't be GC'ed away
        if (selection_buttons == null) {
            selection_buttons = new UIBarButtonItem [] { 
                new UIBarButtonItem ("Deselect All", UIBarButtonItemStyle.Plain, delegate {
                    Process (s.Elements, false);
                }),
                new UIBarButtonItem ("Select All", UIBarButtonItemStyle.Plain, delegate {
                    Process (s.Elements, true);
                })
            };
        }

        dv.NavigationItem.SetRightBarButtonItems (selection_buttons, true);
        NavigationController.PushViewController (dv, true);             
    }

享受MonoTouch(和MonoTouch.Dialog)的乐趣!

答案 1 :(得分:2)

您可以从developer.apple.com结帐this demo。希望它对你有所帮助。