2016-08-12 66 views
0

我對C#完全陌生,並從xamarin IOS代碼開始。我基本上來自本機應用程序背景。 我想要一個簡單的應用程序,演示如何在xamarin中使用swTableView控制器來顯示下面提到的表格。如何在Xamarin IOS中使用swTableView。

像:

ColumnNam ColumnNam ColumnNam ColumnNam 
data1  data2  data3  data4 
data5  data6  data7  data8 

我試圖尋找的例子,但我事先沒有找到一個......如果任何一個已經擁有信息,請讓我知道..

THX

回答

0

沒有系統控制可以執行你需要的效果,你必須定製一個,我寫一個樣本供你參考:

在ViewController.cs中:

public override void ViewDidLoad() 
    { 
     this.Title = "testTalbeView"; 
     this.View.Frame = UIScreen.MainScreen.Bounds; 
     this.View.BackgroundColor = UIColor.White; 

     UITableView tableView = new UITableView (UIScreen.MainScreen.Bounds); 
     tableView.SeparatorStyle = UITableViewCellSeparatorStyle.None; 
     tableView.Source = new MyTableSource(); 
     this.Add (tableView); 
    } 

MyTableSource.cs:

public class MyTableSource : UITableViewSource 
{ 
    private string cellID = "MyCell"; 
    private int columns = 2; 
    private List<string> dataList; 

    public MyTableSource() 
    { 
     dataList = new List<string>(); 
     for (int i = 0; i < 10; i++) { 
      dataList.Add ("data " + i.ToString()); 
     } 
    } 

    #region implemented abstract members of UITableViewSource 

    public override nint RowsInSection (UITableView tableview, nint section) 
    { 
     return dataList.Count/columns + 1; 
    } 

    public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath) 
    { 
     MyCell cell = tableView.DequeueReusableCell (cellID) as MyCell; 
     if (null == cell) { 
      cell = new MyCell (UITableViewCellStyle.Default, cellID); 
      cell.TextLabel.TextAlignment = UITextAlignment.Center; 
     } 

     int row = (int)indexPath.Row; 
     if (0 == row) { 
      cell.SetData ("Column0", "Column1"); 
     } 
     else{ 
      cell.SetData (dataList [(row-1) * columns], dataList [(row-1) * columns + 1]); 
     } 
     return cell; 
    } 

    #endregion 
} 

MyCell.cs:

public class MyCell : UITableViewCell 
{ 
    private UILabel lbC0; 
    private UILabel lbC1; 

    public MyCell (UITableViewCellStyle style,string cellID):base(style,cellID) 
    { 
     lbC0 = new UILabel(); 
     lbC0.TextAlignment = UITextAlignment.Center; 
     this.AddSubview (lbC0); 

     lbC1 = new UILabel(); 
     lbC1.TextAlignment = UITextAlignment.Center; 
     this.AddSubview (lbC1); 
    } 

    public void SetData(string str0,string str1) 
    { 
     lbC0.Text = str0; 
     lbC1.Text = str1; 
    } 

    public override void LayoutSubviews() 
    { 
     nfloat lbWidth = this.Bounds.Width/2; 
     nfloat lbHeight = this.Bounds.Height; 
     lbC0.Frame = new CoreGraphics.CGRect (0, 0, lbWidth, lbHeight); 
     lbC1.Frame = new CoreGraphics.CGRect (lbWidth, 0, lbWidth, lbHeight); 
    } 
} 

然後你就可以得到的tableView喜歡的圖片:

effect image

希望它能夠 幫你。