Xamarin.iOS-UITableView详细使用说明【控件篇】

来源:互联网 发布:linux 从文件向vim复制 编辑:程序博客网 时间:2024/06/05 07:26

前言

 Xamarin.iOS中的UITableView方法和OC或者swift中的UITableView方法一直,不一样的就是书写的语法了,相信学过的一看就会明白,所以这里我只是展示使用,不会过多解释某个方法是用来干什么的。

1.创建UITableView

新建一个UITableView和数据源数组表;

private List<string> premTeams;
private UITableView tableView;

然后初始化UITableView并添加到视图上和设置假数据;

public override void ViewDidLoad(){base.ViewDidLoad();// Perform any additional setup after loading the view, typically from a nib.uiConfig();}public void uiConfig(){premTeams = new List<string>() {"我是数据","我是数据","我是数据","我是数据","我是数据","我是数据","我是数据","我是数据"};tableView = new UITableView();tableView.Frame = this.View.Bounds;//tableView.RegisterNibForCellReuse(MyCell.Nib, "MyCell");tableView.Source = new myViewSource(premTeams,this);this.View.AddSubview(tableView);}

2.实现协议(代理)方法

private class myViewSource : UITableViewSource{private List<string> dumpPremTeams;ViewController owner;public myViewSource(List<string> prems, ViewController owner){dumpPremTeams = prems;this.owner = owner;}//返回行数public override nint RowsInSection(UITableView tableview, nint section){return dumpPremTeams.Count;}//行高public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath){return 50;}//public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath){UITableViewCell theCell = new UITableViewCell();theCell.TextLabel.Text = dumpPremTeams[indexPath.Row];theCell.TextLabel.Font = UIFont.SystemFontOfSize(15);theCell.ImageView.Image = UIImage.FromFile("Mine_Match@2x");//设置单元格的标记theCell.Accessory = UITableViewCellAccessory.DisclosureIndicator;//添加其他视图UISwitch mySwitch = new UISwitch();if (indexPath.Row % 2 == 0) {theCell.AccessoryView = mySwitch;if (indexPath.Row == 2 || indexPath.Row == 4) { mySwitch.On = true;}}return theCell;}//选中某一行public override void RowSelected(UITableView tableView, NSIndexPath indexPath){tableView.DeselectRow(indexPath, true);MyViewController mvc = new MyViewController();UINavigationController nvc = new UINavigationController(mvc);owner.PresentViewController(nvc, true, () => Console.WriteLine("nvc"));}//删除某一行public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath){if (editingStyle == UITableViewCellEditingStyle.Delete){// Delete the row from the data source.dumpPremTeams.RemoveAt(indexPath.Row);owner.tableView.DeleteRows(new[] { indexPath }, UITableViewRowAnimation.Fade);}else if (editingStyle == UITableViewCellEditingStyle.Insert){// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.}} public override string TitleForHeader(UITableView tableView, nint section){return @"this is header";} public override string TitleForFooter(UITableView tableView, nint section){return @"this is footer";}}

3.自定义cell(xib)

我在cell上脱了两个控件,一个UILabel命名myTextLabel,一个UIImageview命名myImageView


然后在cs文件中设置标签属性;

public partial class MyCell : UITableViewCell{public static readonly NSString Key = new NSString("MyCell");public static readonly UINib Nib;static MyCell(){Nib = UINib.FromName("MyCell", NSBundle.MainBundle);}//设置myTextLabel标签属性[Outlet("myTextLabel")]public UILabel myTextLabel{get;private set;}//设置imageview的图形化属性[Outlet("myImageView")]public UIImageView myImageView{get;private set;}protected MyCell(IntPtr handle) : base(handle){// Note: this .ctor should not contain any initialization logic.}}

4.调用自定义cell

tableView.RegisterNibForCellReuse(MyCell.Nib, "MyCell");

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath){MyCell theCell = (MyCell)tableView.DequeueReusableCell(MyCell.Key);theCell.myTextLabel.Text = dumpPremTeams[indexPath.Row]; theCell.myImageView.Image = UIImage.FromFile("Mine_Match@2x");return theCell;}

5.运行效果

系统cell运行


自定义cell运行效果


6.DEMO下载

点击下载

http://download.csdn.net/detail/u014220518/9709942


0 0