Xamarin ios开发 UITableView



UITableView Xamarin 使用总结

 

1.  Rectangle 区域,与其他ui相同,声明时候必须给他分配一块区域

2.  TableView 的类型

    1. plain 

            plain的话每个section之间无分隔

          2. group

          group 每个section之间都有footerheader

3.Delegate

如果想要为TableView Cell添加点击事件的话必须要自己实现一个类继承自

UITableViewDelegate,并且实现 RowSelected 函数,其中的indexpath

可以让你区分tableview的点击事件是属于哪一个cell的。每个indexpath都饱含   sectionrow属性,例如第一个section的第一行 就是 index.section== 0 &&indexpath.row == 0

 

4.DataSource

  每一个tableview都需要一个数据来源你必须要实现一个类来给这个tableview添加数据来源这个类必须得继承自UITableViewDataSource。必须实现这几类函数:

     1. RowsInSection

     这是告诉tableview你的每一个section都有多少个row,即多少个cell

     它传入了一个section参数,你可以利用它来告诉tableview有每个section由多少个row

      2.NumberOfSections

      顾名思义,这是告知tableview有多少section

      3.GetCells

      在这里可设置每个cell的样式,文字内容等。有一些套路的语句:

      UITableViewCell cell =tableView.DequeueReusableCell (kCellIdentifier); if (cell == null) {                                              

                   cell= new UITableViewCell(UITableViewCellStyle.Default,kCellIdentifier);

}

   这里的kCellIndentifier是一开始设置的一个string这个值是全局唯一的,用于区分每个tableview

      cell的文字内容是修改 cell.TextLabel.Text

      cell的文字大小是修改 cell.TextLabel.Font

      cell的图片,如icon之类的修改cell.imageview.image

       另外cell有个Accessory属性,是为了区别cell按钮的功能,可以设置类型

       字体是居中还是居左还是居右是设置cell.textlabel.textalignment

using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace TableViewDemo
{
public partial class TableViewDemoViewController : UIViewController
{
public TableViewDemoViewController (IntPtr handle) : base (handle)
{
}

public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();

// Release any cached data, images, etc that aren't in use.
}

#region View lifecycle

private UITableView tableView;
//private List<string> list;

public override void ViewDidLoad ()
{
base.ViewDidLoad ();

// Perform any additional setup after loading the view, typically from a nib.
List<string> list = new List<string> () {
"Create Expense",
"Create Expense Request",
"My Expense",
"Expense Request",
"Crop Credit Card Request",
"Log out"
};
tableView = new UITableView (new RectangleF (0, 0, this.View.Frame.Width, this.View.Frame.Height), UITableViewStyle.Grouped) {
Delegate = new TableViewDelegate (list, this),
DataSource = new TableViewDataSource (list, this),
//AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth,
BackgroundColor = UIColor.FromRGB(230,230,230)
};
//tableView.SizeToFit ();
UIView headerView = new UIView () {
Frame = new RectangleF(10,20,320,110),
BackgroundColor = UIColor.FromRGB(230,230,230)
};

//UIView ui
UIImageView imageView = new UIImageView () {
Frame = new RectangleF (25, 30, 60, 60),
Image = new UIImage ("1.jpg")
};

UILabel nameLabel = new UILabel (){
Text = "Nash",
Font = UIFont.SystemFontOfSize(20),
Frame = new RectangleF(imageView.Frame.X + imageView.Frame.Width + 20, imageView.Frame.Y,40,40)
};
nameLabel.SizeToFit ();

UILabel officeLabel = new UILabel (){
Text = "Office:ShangHai",
Font = UIFont.SystemFontOfSize(10),
Frame = new RectangleF(imageView.Frame.X + imageView.Frame.Width + 20, nameLabel.Frame.Y + nameLabel.Frame.Height + 5,40,40)
};
officeLabel.SizeToFit ();

UILabel departmentLabel = new UILabel (){
Text = "Department:Develop",
Font = UIFont.SystemFontOfSize(10),
Frame = new RectangleF(imageView.Frame.X + imageView.Frame.Width + 20, officeLabel.Frame.Y + officeLabel.Frame.Height + 5,40,40)
};
departmentLabel.SizeToFit ();

headerView.AddSubview (imageView);
headerView.AddSubview (nameLabel);
headerView.AddSubview (officeLabel);
headerView.AddSubview (departmentLabel);
tableView.TableHeaderView = headerView;

this.View.AddSubview (tableView);

}

public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
}

public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
}

public override void ViewWillDisappear (bool animated)
{
base.ViewWillDisappear (animated);
}

public override void ViewDidDisappear (bool animated)
{
base.ViewDidDisappear (animated);
}

#endregion

private class TableViewDelegate : UITableViewDelegate
{
private List<string> list;
TableViewDemoViewController self;

public TableViewDelegate (List<string> list, TableViewDemoViewController ctrl)
{
this.list = list;
self = ctrl;
}

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
//self.PerformSegue ("sg_root_UILabel", self);
if (indexPath.Section == 0) {
UIAlertView alert = new UIAlertView ("Title", "You choose the first!", null, "OK", null);
alert.Show ();
}else {
UIAlertView alert = new UIAlertView ("Title", "You choose It!", null, "OK", null);
alert.Show ();
}


}

// public override UIView GetViewForHeader (UITableView tableView, int section)
// {
// // NOTE: Don't call the base implementation on a Model class
// // see http://docs.xamarin.com/guides/ios/application_fundamentals/delegates,_protocols,_and_events
// UIView headerView = new UIView () {
// Frame = new RectangleF(0,0,320,40),
// BackgroundColor = UIColor.Green
// };
//
// if (section == 0) {
// headerView.Frame = new RectangleF (15, 15, self.View.Frame.Width, 120);
// UIImageView imageView = new UIImageView () {
// Frame = new RectangleF (15, 20, 50, 50),
// Image = new UIImage ("1.jpg")
// };
// headerView.AddSubview (imageView);
// }
//
// return headerView;
// }
}

private class TableViewDataSource : UITableViewDataSource
{
static NSString kCellIdentifier = new NSString ("MyIdentifier");
private List<string> list;
TableViewDemoViewController self;

public TableViewDataSource (List<string> list, TableViewDemoViewController ctrl)
{
this.list = list;
self = ctrl;
}

public override int RowsInSection (UITableView tableview, int section)
{
switch (section) {
case 0:
return 2;
//break;
case 1:
return 3;
case 2:
return 1;
default:
return 0;
}
}

public override int NumberOfSections (UITableView tableView)
{
return 3;
}

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (kCellIdentifier);
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Default, kCellIdentifier);
}

cell.ImageView.Image = new UIImage ("2.jpg");
cell.TextLabel.Font = UIFont.SystemFontOfSize (15);
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;

switch (indexPath.Section)
{
case 0:
cell.TextLabel.Text = list [indexPath.Row];
break;
case 1:
cell.TextLabel.Text = list [indexPath.Row + 2];
break;
case 2:
cell.TextLabel.Text = list [indexPath.Row + 5];
cell.ImageView.Image = null;
cell.TextLabel.Font = UIFont.SystemFontOfSize (20);
cell.TextLabel.TextAlignment = UITextAlignment.Center;
cell.Accessory = UITableViewCellAccessory.None;
break;
}
return cell;
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值