mono touch:Customizing a Table's Appearance
来源:互联网 发布:淘宝怎么返利 编辑:程序博客网 时间:2024/05/17 07:40
Customizing a Table's Appearance
The simplest way to change the appearance of a table is to use a different cell style. You can change which cell style is used when creating each cell in the UITableViewSource
’s GetCell
method.
UITableViewCell Styles
There are four built-in styles:
- Default – supports an ImageView
- Subtitle – supports an ImageView and subtitle
- Value1 – right aligned subtitle, supports an ImageView
- Value2 – title is right-aligned and subtitle is left-aligned (but no image)
These screenshots show how each style appears.
The sample CellDefaultTable contains the code to produce these screens. The cell style is set in theUITableViewCell
constructor, like this:
cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);//cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);//cell = new UITableViewCell (UITableViewCellStyle.Value1, cellIdentifier);//cell = new UITableViewCell (UITableViewCellStyle.Value2, cellIdentifier);
The cell’s properties can then be set (do not set properties that the style does not support or an exception will be thrown):
cell.TextLabel.Text = tableItems[indexPath.Row].Heading;cell.DetailTextLabel.Text = tableItems[indexPath.Row].SubHeading;cell.ImageView.Image = UIImage.FromFile("Images/"+tableItems[indexPath.Row].ImageName); // don't use for Value2
Accessories
Cells can also have these accessories added to the right of the view
- Checkmark – can be used to indicate ‘selection’ in a table.
- DisclosureIndicator – normally used to indicate that touching the cell will open another view.
- DetailDisclosureIndicator – responds to touch independently of the rest of the cell, allowing it to perform a different function to touching the cell itself.
This is what they look like:
To display one of these accessories you can set the Accessory
property in the GetCell
method:
cell.Accessory = UITableViewCellAccessory.Checkmark;//cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;//cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton; // implement AccessoryButtonTapped//cell.Accessory = UITableViewCellAccessory.None; // to clear the accessory
When the DetailDisclosureButton
is shown you should also override the AccessoryButtonTapped
to perform some action when it is touched.
public override void AccessoryButtonTapped (UITableView tableView, NSIndexPath indexPath){ new UIAlertView("DetailDisclosureButton Touched" , tableItems[indexPath.Row].Heading, null, "OK", null).Show();}
Creating custom cell layouts
To change the visual style of a table you need to supply custom cells for it to display. The custom cell can have different colors, control layout,
The CellCustomTable example implements a UITableViewCell
subclass that defines a custom layout ofUILabels
and a UIImage
with different fonts and colors. The resulting cells look like this:
The custom cell class consists of only three methods:
- Constructor – creates the UI controls and sets the custom style properties (eg. font face, size and colors).
- UpdateCell – a method for
UITableView.GetCell
to use to set the cell’s properties. - LayoutSubviews – set the location of the UI controls. In the example every cell has the same layout, but a more complex cell (particularly those with varying sizes) might need different layout positions depending on the content being displayed.
The complete sample code in CellCustomTable/CustomVegeCell.cs follows:
public class CustomVegeCell : UITableViewCell { UILabel headingLabel, subheadingLabel; UIImageView imageView; public CustomVegeCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId) { SelectionStyle = UITableViewCellSelectionStyle.Gray; ContentView.BackgroundColor = UIColor.FromRGB (218, 255, 127); imageView = new UIImageView(); headingLabel = new UILabel () { Font = UIFont.FromName("Cochin-BoldItalic", 22f), TextColor = UIColor.FromRGB (127, 51, 0), BackgroundColor = UIColor.Clear }; subheadingLabel = new UILabel () { Font = UIFont.FromName("AmericanTypewriter", 12f), TextColor = UIColor.FromRGB (38, 127, 0), TextAlignment = UITextAlignment.Center, BackgroundColor = UIColor.Clear }; ContentView.Add (headingLabel); ContentView.Add (subheadingLabel); ContentView.Add (imageView); } public void UpdateCell (string caption, string subtitle, UIImage image) { imageView.Image = image; headingLabel.Text = caption; subheadingLabel.Text = subtitle; } public override void LayoutSubviews () { base.LayoutSubviews (); imageView.Frame = new RectangleF(ContentView.Bounds.Width - 63, 5, 33, 33); headingLabel.Frame = new RectangleF(5, 4, ContentView.Bounds.Width - 63, 25); subheadingLabel.Frame = new RectangleF(100, 18, 100, 20); }}
The GetCell
method of the UITableViewSource
needs to be modified to create the custom cell:
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath){ CustomVegeCell cell = tableView.DequeueReusableCell (cellIdentifier) as CustomVegeCell; if (cell == null) cell = new CustomVegeCell (cellIdentifier); cell.UpdateCell (tableItems[indexPath.Row].Heading , tableItems[indexPath.Row].SubHeading , UIImage.FromFile ("Images/" +tableItems[indexPath.Row].ImageName) ); return cell;}
- mono touch:Customizing a Table's Appearance
- Customizing a Control's Appearance
- mono touch: Appearance API
- Mono Touch Table应用
- 《Mono: A Developer’s Notebook》
- Customizing Appearance by Using Windows® Presentation Foundation
- Customizing the Appearance of CSliderCtrl Using Custom Draw
- Customizing a Text Editor
- mono touch登录设计
- Mono Touch文件下载
- mono touch:Displaying Images
- mono touch :Displaying Alerts
- mono touch隐藏键盘
- mono touch 隐藏键盘
- mono touch中touch触发对象判断
- mono touch中自定义事件
- mono touch后端事件处理
- mono touch 中图片的StretchableImage属性
- 深入浅出 - Android系统移植与平台开发(五)- 定制手机模拟器ROM
- C语言中的可变参数函数的浅析(以Arm 程序中的printf()函数实现为例) .
- 喜欢那种起落的艰难和空洞
- linux shell if等注意事项
- mysql存储用户表结构的系统表
- mono touch:Customizing a Table's Appearance
- 【block编程第一篇】 block编程热点介绍(官方文档翻译的)
- 练习:创建一个按钮,点击一次,则创建一个<input type="text"><input> 点击三次按钮,应该有三个输入框被创建出来。
- OpenCV+vs2008+摄像头来实时的识别人脸(不是检测)
- Linux下scp命令详解
- Android 虚拟按键驱动实现
- http接口自动化测试框架实现
- Linux下查看在线用户
- CSharp学习笔记之八 文件的读写