2013-08-26 31 views
1

我有一個要求,我最初有一個只按日期/時間排序的消息列表。要求用戶能夠點擊一個UISegmentedControl(4個按鈕列表)並能夠將UITableView從一個直線列表更改爲一個分組列表(即按消息類別分組)。將UITableView從列表切換到分組的最佳方式是什麼?

從我讀過的內容來看,一旦在UITableView上設置了樣式,就無法更改它。那麼滿足這一要求的最佳方法是什麼?殺死視圖並用適當的樣式重新創建?

不,它使一個巨大的差異,我使用Xamarin Studio和C#,針對單3.2.1和iOS 6+

+0

我會盡量只創建一個分組的UITableView並有一個在所有郵件組並隱藏該組標題。我認爲這有點破解,所以我仍然樂於接受任何更好的方法。 –

回答

4

而不是殺死的觀點,並重新實例化,只是保持兩個UITableView S變量,每種適當類型之一。使用你的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"); 
    } 
} 
+0

真棒答案,我已經開始朝着那個方向(有一些差異),但我喜歡你所做的。我會嘗試這個並報告回來。謝謝! –

相關問題