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

发布于 2024-12-12 04:33:30 字数 169 浏览 0 评论 0原文

我想要桌面视图附件按钮,例如全选或取消全选。
cell.Accessory = UITableViewCellAccessory.None; 我想要一个按钮示例:“全选” 当用户触摸此按钮时,每个单元的配件都应选中。 或者我想要“重置”按钮。如果用户触摸此按钮,每个复选标记都会消失,并且 Cell 的配件不会消失。

I want to tableview accessory button like selectall or deselectall.
cell.Accessory = UITableViewCellAccessory.None;
I want to a button example :"select all"
When user touch this button everycell's accessory should checkmark.
Or I want "Reset" button. if user touch this button every checkmark disappear and Cell's accesory shoul none.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余厌 2024-12-19 04:33:30

您可能已经发现使用 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)的乐趣!

As you have likely found out using UITableView is a bit complicated. However there's a wonderful library available for MonoTouch called MonoTouch.Dialog that makes things a lot easier.

The following sample code is using MonoTouch.Dialog to answer your question (as much as I understand it, let me know if my answer does not match what you wanted).

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

Have fun with MonoTouch (and MonoTouch.Dialog)!

伊面 2024-12-19 04:33:30

您可以签出 此演示来自developer.apple.com。希望对您有帮助。

You can checkout this demo from developer.apple.com. Hope it help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文