c# - 将 UITableView 从列表切换到分组的最佳方法是什么?

标签 c# ios uitableview xamarin.ios xamarin

我有一个要求,我最初有一个仅按日期/时间排序的消息列表。要求用户能够单击 UISegmentedControl(4 个按钮的列表)并能够将 UITableView 从直接列表更改为分组列表(即按消息类别分组)。

据我所知,一旦在 UITableView 上设置了样式,就无法更改它。那么满足此要求的最佳方法是什么?终止 View 并使用适当的样式重新创建?

并不是说它有很大的不同,我正在使用 Xamarin Studio 和 C#,目标是 Mono 3.2.1 和 iOS 6+

最佳答案

与其终止 View 并重新实例化,不如维护对两个 UITableView 的引用,每个适当的类型之一。使用您的 Controller 类在它们之间切换。下面的简单示例将切换按钮放在与表格相同的 UIView 中,这可能不合适,但以其他方式展示了该技术:

public class ChangeableSource : UITableViewSource
{
    public bool Grouped { get; set; }

    public override int NumberOfSections(UITableView tableView)
    {
        if(Grouped)
        {
            return 4;
        }
        else
        {
            return 1;
        }
    }

    public override int RowsInSection(UITableView tableview, int section)
    {
        return 3;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell("Default");
        if(cell == null)
        {
            cell = new UITableViewCell(UITableViewCellStyle.Default, "Default");
        }
        cell.TextLabel.Text = String.Format("IndexPath {0} {1}", indexPath.Section, indexPath.Row);
        return cell;
    }
}

public class ToggleTableView : UIView
{
    UITableView ungroupedView;
    UITableView groupedView;
    ChangeableSource changeableSource;

    public void SetStyle(bool grouped)
    {
        changeableSource.Grouped = grouped;
        if(changeableSource.Grouped)
        {
            ungroupedView.RemoveFromSuperview();
            AddSubview(groupedView);
        }
        else
        {
            groupedView.RemoveFromSuperview();
            AddSubview(ungroupedView);
        }
    }

    public bool GetStyle()
    {
        return changeableSource.Grouped;
    }

    public ToggleTableView()
    {
        var btn = new UIButton(new RectangleF(10, 10, 150, 40));
        btn.SetTitle("Change", UIControlState.Normal);
        btn.TouchUpInside += (s,e) => ToggleStyle(this, new EventArgs());

        var tvFrame = new RectangleF(0, 60, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height - 60);
        ungroupedView = new UITableView(tvFrame, UITableViewStyle.Plain);
        groupedView = new UITableView(tvFrame, UITableViewStyle.Grouped);
        AddSubview(btn);
        AddSubview(ungroupedView);

        changeableSource = new ChangeableSource();
        changeableSource.Grouped = false;
        ungroupedView.Source = changeableSource;
        groupedView.Source = changeableSource;
    }

    public event EventHandler<EventArgs> ToggleStyle = delegate {};
}

public class TogglingTableController : UIViewController
{
    public TogglingTableController() : base ()
    {
    }

    public override void DidReceiveMemoryWarning()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning();
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var view = new ToggleTableView();
        view.ToggleStyle += (s,e) => 
        {
            view.SetStyle(! view.GetStyle());
        };

        this.View = view;
    }
}

[Register ("AppDelegate")]
public  class AppDelegate : UIApplicationDelegate
{
    UIWindow window;
    TogglingTableController viewController;

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        window = new UIWindow(UIScreen.MainScreen.Bounds);

        viewController = new TogglingTableController();
        window.RootViewController = viewController;

        window.MakeKeyAndVisible();

        return true;
    }
}

public class Application
{
    static void Main(string[] args)
    {
        UIApplication.Main(args, null, "AppDelegate");
    }
}

关于c# - 将 UITableView 从列表切换到分组的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18449799/

相关文章:

iphone - 如何使 UISegmentedControl 的选定部分变暗?

ios - NSLog 默认断行

ios - 在 NSOperation 中设置 UIImage 时内存泄漏

ios - index ScrollTo 不适用于 TableViewCell Swift 中的最后一个 CollectionView

iphone - 每个 TableView Cell 都有一个图像,有些是 `hidden` ,我如何测试它们是否都没有隐藏?

c# - 在不使用LINQ的通用Windows库中使用SQLite

c# - 在 ASP.NET 中使用依赖注入(inject)和工厂模式传递服务

xcode - 多个 UITableView 但有些行比其他行小

c# - 在ForEach语句中使用linq

c# - 如何检测键盘是否有数字 block ?