Skip to main content
 首页 » 编程设计

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

2024年02月13日17pengyingh

我有一个要求,我最初有一个仅按日期/时间排序的消息列表。要求用户能够单击 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"); 
    } 
}