【问题标题】:how to use swTableView in Xamarin IOS.如何在 Xamarin IOS 中使用 swTableView。
【发布时间】:2016-08-14 11:27:26
【问题描述】:

我对 C# 完全陌生,从 xamarin IOS 代码开始。我基本上来自本机应用程序背景。 我想要一个简单的应用程序来展示如何在 xamarin 中使用 swTableView 控制器来显示如下所述的表格。

喜欢:

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

我尝试搜索示例但我没有找到...如果有人已经有信息请告诉我..

提前谢谢

【问题讨论】:

    标签: xamarin xamarin.ios


    【解决方案1】:

    没有系统控件可以实现你需要的效果,你必须自定义一个,我写一个示例供你参考:

    在 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了:

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      相关资源
      最近更新 更多