C# iOS Xamarin tableview

来源:互联网 发布:百合 知乎 编辑:程序博客网 时间:2024/06/16 19:24

                                                             Xamarin开发iOS

       今天,偶然的发现C#能够开发iOS了。到网上查阅了一下资料,原来是Xamarin团队做的跨平台开发技术,最近被微软收购了,最重要的是已经和VS结合了,而且个人版本的开发环境已经不需要花钱了,想必大家一听到免费,大家都会比较感兴趣,哈哈,屌丝我也是其中一员呢。在mac系统上面安装xamarin是非常简单的,首先安装Xcode,因为要用到Xcode的一些东西(模拟器),然后到官网上面下载,按照教程安装就行,安装环境需要的一些插件,都会提醒我们下载的,傻瓜似的安装。

     下面,我来给大家做一个小小的demo,在iOS开发中,tableview我们是用的非常多的。下面的例子就是用C#开发一个tableview,实现一级二级数据的现实。

下面的这个是数据源的class;

using System;using UIKit;using Foundation;using System.Collections.Generic;namespace Expland{public class MyTableViewSource:UITableViewSource{string cellReuseId="cellReuseId";List<string> firstTitle;//构造函数,传递参数public MyTableViewSource (List<string> firstTitle){this.firstTitle = firstTitle;}//每一行的内容public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath){UITableViewCell cell = tableView.DequeueReusableCell (cellReuseId);if (cell == null) {cell = new UITableViewCell (UITableViewCellStyle.Default, cellReuseId);}cell.TextLabel.Text = "123";return cell;}//每一个section的行数public override nint RowsInSection (UITableView tableview, nint section){return 1;}//section的个数public override nint NumberOfSections (UITableView tableView){return firstTitle.Count;}//section要现实的内容public override nfloat GetHeightForHeader (UITableView tableView, nint section){return 44f;}//section的高度public override UIView GetViewForHeader (UITableView tableView, nint section){UITableViewHeaderFooterView headerView = new UITableViewHeaderFooterView ();headerView.TextLabel.Text=firstTitle[(int) section];headerView.TextLabel.TextColor = UIColor.Black;return headerView;}}}


using System;using Foundation;using System.Collections.Generic;using UIKit;namespace Expland{public partial class ViewController : UIViewController{UITableView tableview;List<string> firstTitle;List<Data> secondTitile;public ViewController (IntPtr handle) : base (handle){}public override void ViewDidLoad (){base.ViewDidLoad ();firstTitle = new List<string> (){ "A", "B", "C", "D", "E", "F" };InitTableView ();InitNavigation ();//tableview的数据源this.tableview.Source = new MyTableViewSource (firstTitle);}//tableview初始化void InitTableView (){tableview = new UITableView (new CoreGraphics.CGRect (0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height));this.View.AddSubview(tableview);}//导航栏初始化void InitNavigation(){this.NavigationItem.Title = "首页";}public override void DidReceiveMemoryWarning (){base.DidReceiveMemoryWarning ();}}}



0 0
原创粉丝点击