仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 545|回复: 8
打印 上一主题 下一主题

[学习教程] ASP.NET网页设计在MonoTouch中自界说表格

[复制链接]
冷月葬花魂 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:19:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
效率会有不少的变化。而实际上java是基于堆栈机器来设计,这和我们常见的基于寄存器的本地机器是差异比较大的。总体来说,这是一种虚拟机的设计思路。为何要定制表格?  表格在良多iPhone使用程序中都是必须的UI元素。固然关于使用程序开辟而言,这并不是是一项新创造,鉴于设备尺寸等方面的限定,表格在iPhone中的功效长短常流动的。
  苹果在其SDK中,间接内置了良多作风来让你定制表格。不外,在你最后创立表格的时分,它看起来十分复杂。在没有举行任何定制的时分,你能够为表格选择两种基础作风,默许作风和分组作风:

  在对表格中的单位格举行一点调剂后,你就能够增加图标和申明笔墨:

  你乃至能改动单位格的字体和色彩,但是,偶然候如许仍是不敷够。假如你真的想完整改动基础的作风,创立一个庞大的UI,那末你必需创立本人的自界说单位格控件。上面的截图是定制后的效果,这个使用程序利用完整定制的单位格来显现内容:

  幸亏,苹果让我们有便利的办法来完成如许的定制,就是同意我们创立本人的定制UITableViewCell控件,并把其用于UITableView控件中。上面,让我们来慢慢创立一个使用程序,进修怎样使用UITableView和UITableViewCell控件来创立上图所示效果的例子。
  示例使用程序
  那末接上去,我们就来创立使用程序。起首,翻开MonoDevelop,以下图所示:

  接着,新建一个iPhoneMonoTouch项目。从“File”菜单中,选择“NewSoluton”:

  从C#iPhone模板当选择“iPhoneWindow-basedProject”,输出办理计划的称号“Example_CustomUITableViewCells”:

  在项目特征对话框上点击“OK”,这些内容和我们要做的使用程序有关:

  我们的项目如今创立好了,以下图所示:

  经由过程在SolutionExplorer窗口里双击MainWindow.xib,以便在InterfaceBuilder中翻开它,以后你会看到以下内容:

  我们起首拖一个UITableView控件到我们的MainWindow中,效果以下:

  我们必要在AppDelegate类中增加一个outlet,以便我们能编程会见这个TableView。
  在Library窗口中,选择顶部的“Classes”标签页,接着鄙人拉列表当选择“OtherClasses”。确保AppDelegate已选中,在Library窗口的下半部点击“Outlets”标签页:

  让我们来创立一个名为“tblMain”的outlet,经由过程点击“+”按钮,在称号列输出“tblMain”,你的outlet应当相似下图的:

  接着,把outlet联系关系到我们之前增加的UITableView上。在Document窗口当选择AppDelegate,在ConnectionsInspector窗口中,就会看到新创立的tblMain outlet:

  从毗连办理器中把右边的outlet的圆点拖动到Document窗口或WindowDesigner的表格上,就能够把它们联系关系到一同:

  如今,我们失掉了一个联系关系过的表格,让我们回到MonoDevelop,编写一些代码。
  在项目上按右键,在高低文菜单当选择“Add”,接着选择“NewFolder”,把文件夹定名为“Code“。
  接着创立一个定名为“BasicTableViewItem.cs”的类。即,在项目上再次按右键,在高低文菜单当选择“Add”,接着选择“NewFile”。定名类,并点击“New”:

  在这个代码文件中,输出以下代码:
namespace Example_CustomUITableViewCells 
{ 
 //======================================================================== 
 /// <summary> 
 /// Represents our item in the table 
 /// </summary> 
 
 public class BasicTableViewItem 
 { 
 public string Name { get; set; } 
  
 public string SubHeading { get; set; } 
  
 public string ImageName { get; set; } 
 
 public BasicTableViewItem () 
 { 
 } 
 } 
 //======================================================================== 
 
}
  这个类相称复杂,它暗示了一个在单位格中的项目,并具有以下属性:
  Name――这个项目所显现的文本
  SubHeading――显现在项目称号上面的文本
  ImageName――这个项目所显现的图片称号
  我们将在前面会看到怎样利用这个类。
  接着,在“Code”文件夹中,创立别的一个类,定名为“BasicTableViewItemGroup”,并输出以下代码:
using System; 
using System.Collections.Generic; 
 
namespace Example_CustomUITableViewCells 
{ 
 //======================================================================== 
 /// <summary> 
 
 /// A group that contains table items 
 /// </summary> 
 public class BasicTableViewItemGroup 
 
 { 
 public string Name { get; set; } 
 
 public string Footer { get; set; } 
  
 public List<BasicTableViewItem> Items 
 { 
  get { return this._items; } 
  set { this._items = value; } 
 } 
 protected List<BasicTableViewItem> _items = new List<BasicTableViewItem>(); 
  
 public BasicTableViewItemGroup () 
 { 
 } 
 } 
 //======================================================================== 
 
} 
  这也是一个相称复杂的类。它用于项目分组的容器。具有以下属性:
  Name――分组的称号。一般显现在分组的顶部。
  Footer――显现在以后组中一切项目标底部笔墨
  Items――分组所包括的BasicTableViewItem的List汇合。我们在其机关的时分实例化这个汇合,以便在增加项目标时分无需手动往实例化它。
  接着,再在统一个文件夹中创立别的一个类,定名为“BasicTableViewSource”,并输出以下代码:
using System; 
using System.Collections.Generic; 
using MonoTouch.UIKit; 
 
namespace Example_CustomUITableViewCells 
{ 
 //======================================================================== 
 /// <summary> 
 
 /// Combined DataSource and Delegate for our UITableView 
 /// </summary> 
 public class BasicTableViewSource : UITableViewSource 
 
 { 
 //---- declare vars 
 protected List<BasicTableViewItemGroup> _tableItems; 
 string _cellIdentifier = "BasicTableViewCell"; 
  
 public BasicTableViewSource (List<BasicTableViewItemGroup> items) 
 { 
  this._tableItems = items; 
 } 
  
 /// <summary> 
 
 /// Called by the TableView to determine how many sections(groups) there are. 
 /// </summary> 
 public override int NumberOfSections (UITableView tableView) 
 { 
  return this._tableItems.Count; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to determine how many cells to create for that particular section. 
 /// </summary> 
 public override int RowsInSection (UITableView tableview, int section) 
 { 
  return this._tableItems[section].Items.Count; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to retrieve the header text for the particular section(group) 
 /// </summary> 
 public override string TitleForHeader (UITableView tableView, int section) 
 { 
  return this._tableItems[section].Name; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to retrieve the footer text for the particular section(group) 
 /// </summary> 
 public override string TitleForFooter (UITableView tableView, int section) 
 { 
  return this._tableItems[section].Footer; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to get the actual UITableViewCell to render for the particular section and row 
 /// </summary> 
 public override UITableViewCell GetCell (UITableView tableView, MonoTouch.FoundationNSIndexPath indexPath) 
 { 
  //---- declare vars 
 
  UITableViewCell cell = tableView.DequeueReusableCell (this._cellIdentifier); 
  
  //---- if there are no cells to reuse, create a new one 
  if (cell == null) 
  { 
  cell = new UITableViewCell (UITableViewCellStyle.Default, this._cellIdentifier); 
  } 
  
  //---- create a shortcut to our item 
 
  BasicTableViewItem item = this._tableItems[indexPath.Section].Items[indexPath.Row]; 
  
  cell.TextLabel.Text = item.Name; 
 
  return cell; 
 } 
 } 
 //======================================================================== 
 
}
  UITableViewSource类卖力处置我们数据的绑定,也处置用户和表格的交互。在我们的例子中,处置用户交互超越本篇文章的局限,不外我们仍然必要完成数据绑定的功效。
  这个类比其他两个略微有点庞大,它的成员一一申明以下:
  变量声明――我们声了然两个变量,_tableItems,用于猎取表格工具的当地变量,_cellIdentifier,在前面在重用表格单位格的时分供应一个关头字。
  机关器――机关器承受一个名为items的参数,其范例是List<BasicTableViewItemGroup>。在我们的例子中,我们强迫类和项目分组一同实例化,以便我们确信他们都是无效的。
  NumberOfSections――这个办法用于猎取在表格中创立的分组数量。在我们的例子中,就前往_tableItems的分组数量。
  RowsInSection――这个办法用于猎取表格中特定分组的项目数量。依据特定的分组索引,前往其包括项目标数目。
  TitleForHeader――这个办法前往某个分组标头的文本。就是前往以后分组工具BasicTableViewItemGroup的Name属性。
  TitleForFooter――这个办法前往某个分组标脚的文本。相似TitleForHeader,不外此次是前往Footer属性。
  GetCell――这个办法依据特定分组和行前往实践的UITableCell控件,这也是数据绑定大批事情产生的中央。起首,我们在相干的UITableView上挪用DequeueReusableCell。如许做次要是由于功能缘故原由。由于iPhone限定了处置器的耗能,假如每次显现都必需创立新的UITableViewCell的话,在碰到很长列表的时分大概会变得十分慢。为懂得决这个成绩,UITableView控件中背景保护着一个UITableViewCell控件缓存池,在单位格转动出可视局限的时分,它会放进到这个缓存池中以备重用。这也就是_cellIdentifier变量的用武的地方。
  好,如今我们编写好了相干的类,就要入手下手增加一些图片,以即可以在项目上显现图标。
  起首,在项目中相似之前创立“Code”文件夹那样,创立一个名为“Images”的文件夹。把以下的图片依照对应的称号一一保留到你的硬盘上:
  图片
  称号

  PawIcon.png

  LightBulbIcon.png

  PushPinIcon.png

  TargetIcon.png

  一旦保留好,就要把他们增加到项目中。在“Images”文件夹上点右键,选择“AddFiles”。选择你保留好的图片,勾中“Overridedefaultbuildaction”,鄙人拉列表当选择“Content”。这很主要。Content是让你在运转时能够加载并利用它们的独一构建举措。
  你的办理计划如今应当以下所示:

  为了确保你之前的步骤都是准确的,能够经由过程编译项目来查验。要举行编译,按住AppleKey(苹果键),再按“B”键,大概从“Build”菜单当选择”BuildAll“。
  统统一般的话,我们就筹办好了相干的类和图片。让我们来编纂使用程序代码以利用它们!
  在Main.cs上双击,显现出代码编纂器。在AppDelegate类中,增加以下代码行:
BasicTableViewSource _tableViewSource; 
  ThisisaclassvariabletoholdourBasicTableViewSourcethatwecreatedearlier.
  这是一个类变量,用来保留我们初期创立的BasicTableViewSource实例。
  接着,把以下办法复制到AppDelegate类中:
/// <summary> 
/// Creates a set of table items. 
/// </summary> 
protected void CreateTableItems () 
{ 
 List<BasicTableViewItemGroup> tableItems = new List<BasicTableViewItemGroup> (); 
 
 //---- declare vars 
 
 BasicTableViewItemGroup tGroup; 
 
 //---- birds 
 tGroup = new BasicTableViewItemGroup() { Name = "Birds", Footer = "Birds have wings, and sometimes use them." }; 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Crow", SubHeading = "AKA, Raven.", ImageName = "PawIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Chicken", SubHeading = "Males are called roosters.", ImageName = "PushPinIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Turkey", SubHeading = "Eaten at thanksgiving.", ImageName = "LightBulbIcon.png" }); 
 tableItems.Add (tGroup); 
 
 //---- fish 
 
 tGroup = new BasicTableViewItemGroup() { Name = "Fish", Footer = "Fish live in water. Mostly." }; 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Trout", SubHeading = "Rainbow is a popular kind.", ImageName = "TargetIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Salmon", SubHeading = "Good sushi.", ImageName = "PawIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Cod", SubHeading = "Flat fish.", ImageName = "LightBulbIcon.png" }); 
 tableItems.Add (tGroup); 
 
 //---- mammals 
 
 tGroup = new BasicTableViewItemGroup() { Name = "Mammals", Footer = "Mammals nurse their young." }; 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Deer", SubHeading = "Bambi.", ImageName = "PushPinIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Bats", SubHeading = "Fly at night.", ImageName = "TargetIcon.png" }); 
 tableItems.Add (tGroup); 
 
 this._tableViewSource = new BasicTableViewSource(tableItems); 
} 
  我们将挪用这个办法来创立数据源,完成在表格中显现数据的历程。
  终极,在AppDelegate类中的FinishedLaunching办法中,在挪用window.MakeKeyAndVisible之前到场以下代码。
this.CreateTableItems (); 
this.tblMain.Source = this._tableViewSource; 
  经由过程挪用这个办法,我们就创立了一个表格数据源,并把数据源赋值给表格。
  终极的Main.cs应当像如许:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
 
namespace Example_CustomUITableViewCells 
{ 
 public class Application 
 
 { 
 static void Main (string[] args) 
 { 
  UIApplication.Main (args); 
 } 
 } 
 
 // The name AppDelegate is referenced in the MainWindow.xib file. 
 public partial class AppDelegate : UIApplicationDelegate 
 
 { 
 BasicTableViewSource _tableViewSource; 
  
 // This method is invoked when the application has loaded its UI and its ready to run 
 public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
 { 
  this.CreateTableItems (); 
  this.tblMain.Source = this._tableViewSource; 
 
  // If you have defined a view, add it here: 
 
  // window.AddSubview (navigationController.View); 
  window.MakeKeyAndVisible (); 
  
  return true; 
 } 
 
 // This method is required in iPhoneOS 3.0 
 public override void OnActivated (UIApplication application) 
 { 
 } 
  
 /// <summary> 
 
 /// Creates a set of table items. 
 /// </summary> 
 protected void CreateTableItems () 
 { 
  List<BasicTableViewItemGroup> tableItems = new List<BasicTableViewItemGroup> (); 
  
  //---- declare vars 
 
  BasicTableViewItemGroup tGroup; 
  
  //---- birds 
  tGroup = new BasicTableViewItemGroup() { Name = "Birds", Footer = "Birds have wings, and sometimes use them." }; 
  tGroup.Items.Add (new BasicTableViewItem() { Name = "Crow", SubHeading = "AKA, Raven.", ImageName = "PawIcon.png" }); 
  tGroup.Items.Add (new BasicTableViewItem() { Name = "Chicken", SubHeading = "Males are called roosters.", ImageName = "PushPinIcon.png" }); 
  tGroup.Items.Add (new BasicTableViewItem() { Name = "Turkey", SubHeading = "Eaten at thanksgiving.", ImageName = "LightBulbIcon.png" }); 
  tableItems.Add (tGroup); 
  
  //---- fish 
 
  tGroup = new BasicTableViewItemGroup() { Name = "Fish", Footer = "Fish live in water. Mostly." }; 
  tGroup.Items.Add (new BasicTableViewItem() { Name = "Trout", SubHeading = "Rainbow is a popular kind.", ImageName = "TargetIcon.png" }); 
  tGroup.Items.Add (new BasicTableViewItem() { Name = "Salmon", SubHeading = "Good sushi.", ImageName = "PawIcon.png" }); 
  tGroup.Items.Add (new BasicTableViewItem() { Name = "Cod", SubHeading = "Flat fish.", ImageName = "LightBulbIcon.png" }); 
  tableItems.Add (tGroup); 
  
  //---- mammals 
 
  tGroup = new BasicTableViewItemGroup() { Name = "Mammals", Footer = "Mammals nurse their young." }; 
  tGroup.Items.Add (new BasicTableViewItem() { Name = "Deer", SubHeading = "Bambi.", ImageName = "PushPinIcon.png" }); 
  tGroup.Items.Add (new BasicTableViewItem() { Name = "Bats", SubHeading = "Fly at night.", ImageName = "TargetIcon.png" }); 
  tableItems.Add (tGroup); 
  
  this._tableViewSource = new BasicTableViewSource(tableItems); 
 } 
 
 } 
} 
  让我们来运转下使用程序,看看甚么模样。要运转使用程序,按住苹果键,并按”Enter”按钮,大概从Run菜单当选择Debug。
  当我们运转它以后,就能够看到下图所示效果:

  脚标看上往稍显奇异。让我们来把表格的范例改成Grouped后从头运转一下。要如许做,必要在InterfaceBuilder中翻开MainWindow.xib,然后在Document窗口上点击TableView来编纂表格作风,接着在Inspector窗口,经由过程作风下拉列表来改动为“Grouped”:

  保留文件,切换回MonoDevelop,再次运转使用程序,你就能够看到以下效果了:

  如今脚标就看着扎眼多了。
  让我们再来处置其他中央。在项目中要显现一些图片和子题目,那末上面就要动手编写。
  中断使用程序,接着双击BasicTableViewSource.cs类来编纂它。在GetCellmethod办法中把以下代码行:
cell = new UITableViewCell (UITableViewCellStyle.Default, this._cellIdentifier); 
  改成:
cell = new UITableViewCell (UITableViewCellStyle.Subtitle, this._cellIdentifier); 
  如许就把UITableViewCell的作风改动为Subtitle,其会在单位格中显现文本的第二行。
  你能选用的只要很少的作风。但是要注重,只要在Default和Subtitle作风中才撑持图片。
  如今,在这行以后:
cell.TextLabel.Text = item.Name; 
  增添:
cell.Accessory = UITableViewCellAccessory.Checkmark; 
cell.DetailTextLabel.Text = item.SubHeading; 
if(!string.IsNullOrEmpty(item.ImageName)) 
{ 
 cell.ImageView.Image = UIImage.FromFile("Images/" + item.ImageName ); 
}
  UITableViewCells的本来模样就会被改动为以下所示:

  UITableViewCell控件底本带有两个次要部分,单位格主体区,其是单位格显现内容的全体地区;单位格内容区,其一般用来安排内容在内里。假如附加区未利用的话,单位格内容区就会扩大占满全部单位格主体区。相似地,假如图片区不利用的话,标签区会掩盖图片的中央。
  让我们来看一下怎样在代码中利用这些地区。第一行代码告知单位格,右侧会有一个Checkmark。接着我们增加SubHeading文本到DetailTextLabel上。最初,我们在单位格上设置一个图片。
  很简单就能够经由过程文件来创立图片工具。复杂地利用UIImage.FromFile机关函数,把图片文件的路径传送就往。因为之前把图片文件的编译范例设为Content,只需项目中的文件夹布局就是文件体系的,那末就能够简单地会见它们。
  如今运转使用程序,就失掉上面的效果:
[img=268style=,500alt=在MonoTouch中自界说表格src=]http://www.ckuyun.com/[/img]
  十分好。假如在InterfaceBuilder中把表格作风改回Plain(双击翻开MainWindow.xib,选择表格,鄙人拉列表当选择作风为“Plain”,并保留),那末再次运转,就失掉:

  仍是很大度的。
  在iPhoneOS3.0以后,我们也能界说一些其他属性,包含TextColor、SelectedTextColor、SelectedImage、BackgroundView、SelectedBackgroundView等等。这固然让你有了更多的本性化设置,不外偶然候,我们必要完整分歧的显现效果。
  要完成这个事情,必需创立本人的表格单位格。苹果同意我们经由过程承继UITableViewCell,并创立个中我们所想的任何内容。
  那末,让我们来创立自界说的表格款式。起首,在项目中创立一个名为“Controls”的新文件夹。在个中,我们盘算用ViewController来创立新视图。在Controls文件夹上点右键,选择“Add”,接着“NewFile”,从左栏选择“iPhone”并在右侧选择“ViewInterfaceDefinitionwithController”,并定名为“CustomTableViewCell”:

  在InterfaceBuilder翻开CustomTableViewCell.xib。就会显现空缺视图:

  我们不想利用尺度视图,而要创立一个UITableViewCell,以是我们会删除这个视图并用一个单位格工具来交换它。
  按住苹果键(Apple),按退格键,大概从编纂窗口当选择“Delete”就能够从Document窗口中删除这个视图。
  接着,从Library窗口中拖一个TableViewCell到你的Document窗口中:

  假如你在新增加到表格单位格上双击,就会翻开计划器,你会看到一个计划界面来让你增加控件:

  那末,如今让我们来利用InterfaceBuilder来计划自界说单位格。拖动右下角的让单位格变高点。接着,从Library窗口,拖一个UIImageView控件和几个UILabels控件:

  你可使用AttributesInspector窗口来改动UILabel控件的巨细,和色彩等等属性。不必看上往完整和上图一样,只需相干的元素都在对应地位就行。
  如今,我们完成了自界说的计划,就来为自界说单位格增加outlet,和File’sOwner的内容,以便我们能会见它们。之前,我们把outlet间接增加到AppDelegate中,不外这里,要把他们增加到File’sOwner中,以即可以在所具有的视图上创立类。
  在Document窗口当选择“File’sOwner”,接着在Library窗口中在顶部选择“Classes”,鄙人拉列表当选择“OtherClasses”。选择我们的CustomTableViewCell类,接着在窗口的下半部选择“Outlets”标签页。增加名为“cellMain”、“lblHeading”、“lblSubHeading”和“imgMain:”的Outlets:

  把Outlets和我们之前增加的控件联系关系起来,就像对表格控件所做的那样。
  确保Document窗口的“File’sOwner“是被选中的,从ConnectionInspector中把圆点拖到Document窗口的控件上:

  最初一件我们必需在InterfaceBuilder中完成的事变,就是供应一个单位格标识。在Document窗口上点击”TableViewCell“,接着在AttributesInspector上把Identifier属性设置为”MyCustomTableCellView“:

  这让我们的自界说单位格模板能够被重用,正如之前我们在代码中完成的那样。
  我们已完成了InterfaceBuilder中事变了,那末保留文件,前往到MonoDevelop中。双击CustomTableCellView.xib.cs翻开这个类文件。
  我们要对这个类举行一点编纂。起首,我们来增加一些属性到类内里:
public UITableViewCell Cell 
{ 
 get { return this.cellMain; } 
} 
 
public string Heading 
{ 
 get { return this.lblHeading.Text; } 
 set { this.lblHeading.Text = value; } 
} 
 
 
public string SubHeading 
{ 
 get { return this.lblSubHeading.Text; } 
 set { this.lblSubHeading.Text = value; } 
} 
 
 
public UIImage Image 
{ 
 get { return this.imgMain.Image; } 
 set { this.imgMain.Image = value; } 
}
  我们来看一下这些属性:
  CellC这个属性让这个自界说单位格的挪用者能够间接会见单位格自己,以便在UITableViewSource的GetCell挪用中,我们能间接从一个属性复杂地前往,过一会我们会看到怎样弄。
  Heading和SubHeadingC这个属性表露了能够间接会见表格单位格中UILabel控件的文本值。
  ImageC这个属性间接表露表格单位格中的UIImage控件。
  接上去,我们必要编纂个中一个机关器。文件在被MonoDevelop创立好后,文件中的最初一个机关器看起来以下:
public CustomTableViewCell () : base("CustomTableViewCell", null) 
{ 
 Initialize (); 
}
  这是一个十分复杂的机关器,假如你读过Mono文档关于基类机关申明的话,就会分明这里它会初始化这个类,并用我们传送出来的称号来加载Nib(编译好的.xib文件)文件。但是,文档中没有提到的是,基类机关器是异步的。假如你利用这类体例来挪用,那末Nib仿佛未被立即加载,在我们给自界说单位格设置诸如Heading属性的时分,我们会失掉厌恶的空援用毛病。
  为了修改这个成绩,我们必需确保Nib文件同步地加载,即直到加载完成,办法是不会前往。
  那末,就必要编纂机关器,以便看起来以下:
public CustomTableViewCell ()// : base("CustomTableViewCell", null) 
{ 
 //---- this next line forces the loading of the xib file to be synchronous 
 MonoTouch.Foundation.NSBundle.MainBundle.LoadNib ("CustomTableViewCell", this, null); 
 Initialize (); 
}
  在这里,我们起首要做到就是,把挪用基类机关器的代码正文失落。我们会手动地举行来挪用谁人代码,以是在这里不必再调一次。接上去,我们增加一个对LoadNib办法的挪用。它实践上和基类机关器所做的事变是一样的,除不会强迫举行异步处置。如今,我们确保了Nib和任何器材都能初始化,以便我们能会见它。
  完全的类看上往以下所示:
using System; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using System; 
 
namespace Example_CustomUITableViewCells 
{ 
 //======================================================================== 
 
 public partial class CustomTableViewCell : UIViewController 
 { 
 #region Constructors 
 
 // The IntPtr and initWithCoder constructors are required for controllers that need 
 
 // to be able to be created from a xib rather than from managed code 
 
 public CustomTableViewCell (IntPtr handle) : base(handle) 
 { 
  Initialize (); 
 } 
 
 [Export("initWithCoder:")] 
 public CustomTableViewCell (NSCoder coder) : base(coder) 
 { 
  Initialize (); 
 } 
 
 public CustomTableViewCell ()// : base("CustomTableViewCell", null) 
 
 { 
  //---- this next line forces the loading of the xib file to be synchronous 
  MonoTouch.Foundation.NSBundle.MainBundle.LoadNib ("CustomTableViewCell", this, null); 
  Initialize (); 
 } 
 
 void Initialize () 
 { 
 } 
  
 #endregion 
 
  
  
 public UITableViewCell Cell 
 { 
  get { return this.cellMain; } 
 } 
  
 public string Heading 
 { 
  get { return this.lblHeading.Text; } 
  set { this.lblHeading.Text = value; } 
 } 
  
 public string SubHeading 
 { 
  get { return this.lblSubHeading.Text; } 
  set { this.lblSubHeading.Text = value; } 
 } 
  
 public UIImage Image 
 { 
  get { return this.imgMain.Image; } 
  set { this.imgMain.Image = value; } 
 } 
  
 } 
 //======================================================================== 
 
}
  好,如今构建好自界说单位格类了,接着往下走,来创立自界说UITableViewSource类。
  在”Code“文件夹中,创立一个新的类,复制以下代码出来:
using System; 
using System.Collections.Generic; 
using MonoTouch.UIKit; 
 
namespace Example_CustomUITableViewCells 
{ 
 //======================================================================== 
 /// <summary> 
 
 /// Combined DataSource and Delegate for our UITableView with custom cells 
 /// </summary> 
 public class CustomTableViewSource : UITableViewSource 
 
 { 
 //---- declare vars 
 protected List<BasicTableViewItemGroup> _tableItems; 
 protected string _customCellIdentifier = "MyCustomTableCellView"; 
 protected Dictionary<int, CustomTableViewCell> _cellControllers = 
  new Dictionary<int, CustomTableViewCell>(); 
  
 public CustomTableViewSource (List<BasicTableViewItemGroup> items) 
 { 
  this._tableItems = items; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to determine how many sections(groups) there are. 
 /// </summary> 
 public override int NumberOfSections (UITableView tableView) 
 { 
  return this._tableItems.Count; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to determine how many cells to create for that particular section. 
 /// </summary> 
 public override int RowsInSection (UITableView tableview, int section) 
 { 
  return this._tableItems[section].Items.Count; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to retrieve the header text for the particular section(group) 
 /// </summary> 
 public override string TitleForHeader (UITableView tableView, int section) 
 { 
  return this._tableItems[section].Name; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to retrieve the footer text for the particular section(group) 
 /// </summary> 
 public override string TitleForFooter (UITableView tableView, int section) 
 { 
  return this._tableItems[section].Footer; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to retreive the height of the row for the particular section and row 
 /// </summary> 
 public override float GetHeightForRow (UITableView tableView, MonoTouch.FoundationNSIndexPath indexPath) 
 { 
  return 109f; 
 } 
 
 /// <summary> 
 
 /// Called by the TableView to get the actual UITableViewCell to render for the particular section and row 
 /// </summary> 
 public override UITableViewCell GetCell (UITableView tableView, MonoTouch.FoundationNSIndexPath indexPath) 
 { 
  //---- declare vars 
 
  UITableViewCell cell = tableView.DequeueReusableCell (this._customCellIdentifier); 
  CustomTableViewCell customCellController = null; 
  
  //---- if there are no cells to reuse, create a new one 
  if (cell == null) 
  { 
  customCellController = new CustomTableViewCell (); 
  // retreive the cell from our custom cell controller 
 
  cell = customCellController.Cell; 
  // give the cell a unique ID, so we can match it up to the controller 
  cell.Tag = Environment.TickCount; 
  // store our controller with the unique ID we gave our cell 
  this._cellControllers.Add (cell.Tag, customCellController); 
  } 
  else 
  { 
  // retreive our controller via it"s unique ID 
 
  customCellController = this._cellControllers[cell.Tag]; 
  } 
  
  //---- create a shortcut to our item 
  BasicTableViewItem item = this._tableItems[indexPath.Section].Items[indexPath.Row]; 
  
  //---- set our cell properties 
 
  customCellController.Heading = item.Name; 
  customCellController.SubHeading = item.SubHeading; 
  if (!string.IsNullOrEmpty (item.ImageName)) 
  { 
  customCellController.Image = UIImage.FromFile ("Images/" + item.ImageName); 
  } 
  
  //---- return the custom cell 
  return cell; 
 } 
 
 } 
 //========================================================================} 
 
}
  假如你扫瞄一遍这个代码,就看到它几近和BasicTableViewSource一样,只要少量改动。
  要注重到第一件事,是声明部分:
//---- declare vars 
protected List<BasicTableViewItemGroup> _tableItems; 
protected string _customCellIdentifier = "MyCustomTableCellView"; 
protected Dictionary<int, CustomTableViewCell> _cellControllers = 
 new Dictionary<int, CustomTableViewCell>();
  我们增加了一个名为_cellControllers新变量。我们将在前面看到怎样利用它,它会保留着自界说单位格的汇合。
  下一个增添的器材是GetHeightForRow办法:
/// <summary> 
/// Called by the TableView to retreive the height of the row for the particular section and row 
/// </summary> 
public override float GetHeightForRow (UITableView tableView, MonoTouch.FoundationNSIndexPath indexPath) 
{ 
 return 109f; 
}
  当我们创立自界说单位格把持器以后,我们让它比一般的单位格要高。在数据绑定的过程当中,CocoaTouch必要晓得要给这个单位格分派几空间。这就是要挪用GetHeightForRow办法的启事。假如我们不告诉新的巨细,那末体系会依照尺度尺寸往调剂结构,我们就会看到奇异的了局。
  假如我们在InterfaceBuilder中翻开我们的单位格,并经由过程SizeInspector检察,会晓得单位格的高度上几。在我们的例子中,它是109像素,不外你的大概会有所分歧。
  最初改动的一块中央是,GetCell办法:
/// <summary> 
/// Called by the TableView to get the actual UITableViewCell to render for the particular section and row 
/// </summary> 
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.FoundationNSIndexPath indexPath) 
{ 
 //---- declare vars 
 
 UITableViewCell cell = tableView.DequeueReusableCell (this._customCellIdentifier); 
 CustomTableViewCell customCellController = null; 
 
 //---- if there are no cells to reuse, create a new one 
 if (cell == null) 
 { 
 customCellController = new CustomTableViewCell (); 
 // retreive the cell from our custom cell controller 
 
 cell = customCellController.Cell; 
 // give the cell a unique ID, so we can match it up to the controller 
 cell.Tag = Environment.TickCount; 
 // store our controller with the unique ID we gave our cell 
 this._cellControllers.Add (cell.Tag, customCellController); 
 } 
 else 
 { 
 // retreive our controller via it"s unique ID 
 
 customCellController = this._cellControllers[cell.Tag]; 
 } 
 
 //---- create a shortcut to our item 
 BasicTableViewItem item = this._tableItems[indexPath.Section].Items[indexPath.Row]; 
 
 //---- set our cell properties 
 
 customCellController.Heading = item.Name; 
 customCellController.SubHeading = item.SubHeading; 
 if (!string.IsNullOrEmpty (item.ImageName)) 
 { 
 customCellController.Image = UIImage.FromFile ("Images/" + item.ImageName); 
 } 
 
 //---- return the custom cell 
 return cell; 
}
  这个办法入手下手十分相似于BasicTableViewSource类,我们实验经由过程DequeueReusableCell的挪用来从缓存池中重用的一个单位格。从这以后,就变得庞大了。假如我们不克不及在重用的时分失掉单位格,那末我们必需往创立一个新的。它就是由我们的CustomTableViewCell类所创立的,它实践上是一个包括着自界说单位格的把持器。
  一旦我们创立了把持器,我们就把cell变量赋值把持器的Cell属性。
  我们接着为单位格增加了一个独一标识符,以便我们在前面能把它和准确的CustomTableViewCell把持器联系关系在一同。我们利用Environment.TickCount,由于它供应了对照稳妥的独一性值。假如我们想,也能够利用Guid.NewGuid,不外挪用它略微有点高贵。
  接上去,我们利用不异的tag标识,来创立存储在CustomTableViewCell的字典工具。
  这也就是我们上面这行代码不言而喻的缘故原由:
else 
{ 
 // retreive our controller via it"s unique ID 
 customCellController = this._cellControllers[cell.Tag]; 
}
  假如我们找到一个可重用的单位格,那末就必要经由过程把持器来设置相干属性。为了如许,必需利用之前我们利用过的标识符从Dictionary中提掏出来。
  到这里,已触及了对这个类的年夜部分改动。接上去的几行,为了改动把持器类中的属性,举行了相对单位格自己较复杂的修正。
  好了!我们将近完成了。翻开Main.cs文件,我们对AppDelegate类举行两个中央的改动,让其利用我们的CustomTableViewSource而非BasicTableViewSource。
  起首,正文失落这行:
BasicTableViewSource _tableViewSource; 
  并紧接着后增加这行:
CustomTableViewSource _tableViewSource; 
  接着,在CreateTableItems办法中,正文失落这行:
this._tableViewSource = new BasicTableViewSource(tableItems); 
  并紧接着前面增加这行:
this._tableViewSource = new CustomTableViewSource(tableItems); 
  我们修正后的AppDelegate类看起来以下:
// The name AppDelegate is referenced in the MainWindow.xib file. 
public partial class AppDelegate : UIApplicationDelegate 
{ 
 //BasicTableViewSource _tableViewSource; 
 
 CustomTableViewSource _tableViewSource; 
 
 // This method is invoked when the application has loaded its UI and its ready to run 
 public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
 { 
 this.CreateTableItems (); 
 this.tblMain.Source = this._tableViewSource; 
 
 // If you have defined a view, add it here: 
 
 // window.AddSubview (navigationController.View); 
 window.MakeKeyAndVisible (); 
  
 return true; 
 } 
 
 // This method is required in iPhoneOS 3.0 
 public override void OnActivated (UIApplication application) 
 { 
 } 
 
 /// <summary> 
 
 /// Creates a set of table items. 
 /// </summary> 
 protected void CreateTableItems () 
 { 
 List<BasicTableViewItemGroup> tableItems = new List<BasicTableViewItemGroup> (); 
  
 //---- declare vars 
 
 BasicTableViewItemGroup tGroup; 
  
 //---- birds 
 tGroup = new BasicTableViewItemGroup() { Name = "Birds", Footer = "Birds have wings, and sometimes use them." }; 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Crow", SubHeading = "AKA, Raven.", ImageName = "PawIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Chicken", SubHeading = "Males are called roosters.", ImageName = "PushPinIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Turkey", SubHeading = "Eaten at thanksgiving.", ImageName = "LightBulbIcon.png" }); 
 tableItems.Add (tGroup); 
 
 //---- fish 
 
 tGroup = new BasicTableViewItemGroup() { Name = "Fish", Footer = "Fish live in water. Mostly." }; 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Trout", SubHeading = "Rainbow is a popular kind.", ImageName = "TargetIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Salmon", SubHeading = "Good sushi.", ImageName = "PawIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Cod", SubHeading = "Flat fish.", ImageName = "LightBulbIcon.png" }); 
 tableItems.Add (tGroup); 
 
 //---- mammals 
 
 tGroup = new BasicTableViewItemGroup() { Name = "Mammals", Footer = "Mammals nurse their young." }; 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Deer", SubHeading = "Bambi.", ImageName = "PushPinIcon.png" }); 
 tGroup.Items.Add (new BasicTableViewItem() { Name = "Bats", SubHeading = "Fly at night.", ImageName = "TargetIcon.png" }); 
 tableItems.Add (tGroup); 
  
 //this._tableViewSource = new BasicTableViewSource(tableItems); 
 
 this._tableViewSource = new CustomTableViewSource(tableItems); 
 } 
} 
  运转这个使用程序,就可以看到上面的效果:

  假如你在InterfaceBuilder中把表格作风改成Grouped,保留改动,再次运转程序,你就会看到下图:

  祝贺!做了这么多的事情,终究完成了,不外你也学到了怎样在MonoTouch中自界说表格。如今,我们已让其运转起来了,来看一下在举行自界说单位格的时分,要思索到一些事项。
  功能思索
  假如没有准确地处置自界说单位格的开辟,在利用UITableView的时分会形成严峻的功能成绩。出格在你有良多行的时分更是云云。iPhone,固然使人印象深入,不外却只要功能无限的处置器,而为了取得用户所希冀的较好呼应才能,你应当思索几本性能优化体例。
  别的,尽早部署到设备上常常性地评价自界说表格的功能,是一种可取的做法。摹拟器就是一个摹拟器罢了,并不是真实的设备。它比设备运转的要快很多,假如你依附于摹拟器来丈量使用程序的功能,那末你极可能会在部署到设备上的时分事与愿违。
  并且,在你测试自界说表格的时分,假如用户能增加本人的行,那末你应当测试使用程序有良多行的功能。假如它仍然“活蹦乱跳”,具有很好的呼应,那末你就可以确信你已开辟准确,而你的用户应当不会对功能扫兴。
  单位格重用-单位格重用是取得具有优秀呼应表格的第一个手艺。出格在具有大批的行的时分,更是必要。假如你不重用单位格,那末iPhoneOS不能不在每一个单位格显现的时分往创立一个新的实例。这即刻就会酿成一个严峻的成绩。为了断定你的单位格是不是正被重用,倡议在单位格重用代码中利用Console.WriteLine把重用的历程打印到使用程序把持台。确保在第一次显现界面的时分,增加了充足多的行,以便在转动屏幕的时分,OS无机会重用老的已被转动出屏幕的单位格。本文章的例子就展现了怎样准确地重用单位格,假如你的单位格没有重用,研讨一下代码示例,确保一切完成都是准确的。
  缓存行高-表格会常常性地猎取行的高度。实践上,它会在每次单位格创立的时分都猎取一遍,在某些时分乃至会更频仍。假如你基于单位格内容来盘算行高,那末确保要缓存这个值,以便你不用老是盘算它。实践上,你大概会在单位格把持器中创立一个属性,盘算,然后缓存之。
  缓存图片-假如你正利用图片,思索缓存它们,以便不用每次显现的时分都从文件中载进。假如单位格之间会共享图片,能够思索把他们放到一个字典工具中,并从内里来猎取图片实例。要当心不要加载太多图片。用到可供你安排的iPhone内存的一半是不太一般的,因而假如你有大批的图片,你就要做出选择,是从文件中加载它们,仍是在缓存字典中只保存必定数目的图片,没有的时分再从文件中加载。
  阔别通明效果-在iPhone上实行的一个十分高贵的操纵就是衬着通明效果。在iPhone上有两个画图体系,CoreGraphics和CoreAnimation。CoreGraphics使用GPU,而CoreAnimation依据要处置内容的快慢也使用主处置器,不外一般次要利用GPU。iPhone的GPU的成绩在于它没有针对色彩夹杂举行优化。因而,你应当尽量实验制止通明效果。假如你的确不克不及制止,那末你应当经由过程重写DrawRect办法来本人完成夹杂历程,并间接处置画图盘算,关于这点我们将鄙人一条中具体讲到。
  手动在DrawRect中绘制单位格-要办理下面提到的功能成绩的最初勉力,就是重写DrawRect办法,本人实行画图。但是这也出缺点。起首,在手艺上绝对庞大,第二,它会占用大批内存。就这点而言,它更合适那些没有大批行的表格,不外如许也会占用大批的处置器资本。对这个手艺更多的信息,能够参考苹果示例程序的第15个例子――TableViewSuite,上面会给出正文。
  制止庞大的图形盘算-正如通明效果成绩一下,只管阔别那些必要盘算的图形元素,好比在显现的时分把图片处置为突变效果。
  编程创立单位格-假如你已千方百计举行优化了,仍然仍是必要进一步提拔功能,那末你能够不必InterfaceBuilder来创立自界说UITableViewCell,而是编程手写这个控件。关于编程构建视图的更多信息,可会见CraigDunn的博客帖子。要进修怎样不利用InterfaceBuilder来创立MonoTouch使用程序,能够浏览MonoTouch.info内里列出的文章,在这里:http://monotouch.info/Tags/NoIB
  展现举行功能优化的更多例子,能够会见苹果的TableViewSuite示例使用程序。
  它固然是用Objective-C编写的,不外观点是不异的。
觉得J2EE好像有很多工具,比如servlet,jboss,tomcat,ejb什么的,可是微软的.NET怎么什么也没有啊?
若相依 该用户已被删除
沙发
发表于 2015-1-19 08:32:00 | 只看该作者
大哥拜托,Java在95年就出来了,微软垄断个妹啊,服务器市场微软完全是后后来者,当年都是Unix的市场,现在被WindowsServer和Linux抢下大片,包括数据库也一样。
莫相离 该用户已被删除
板凳
发表于 2015-1-27 09:42:01 | 只看该作者
但是java靠开源打出的一片天地,特别是在微软的垄断下能打开今天的局面还是有它的生命力的。
精灵巫婆 该用户已被删除
地板
发表于 2015-2-5 08:18:45 | 只看该作者
代码逻辑混乱,难于管理:由于ASP是脚本语言混合html编程,所以你很难看清代码的逻辑关系,并且随着程序的复杂性增加,使得代码的管理十分困难,甚至超出一个程序员所能达到的管理能力,从而造成出错或这样那样的问题。
小女巫 该用户已被删除
5#
发表于 2015-2-11 08:14:50 | 只看该作者
网页从开始简单的hmtl到复杂的服务语言,走过了10多个年头,各种技术层出不穷,单个的主流技术也在不断翻新的版本,现在分析下各种语言的区别、优势、劣势、开发注意事项!
只想知道 该用户已被删除
6#
发表于 2015-3-2 02:00:38 | 只看该作者
由于CGI程序每响应一个客户就会打开一个新的进程,所以,当有多个用户同时进行CGI请求的时候,服务器就会打开多个进程,这样就加重了服务器的负担,使服务器的执行效率变得越来越低下。
山那边是海 该用户已被删除
7#
发表于 2015-3-11 02:05:17 | 只看该作者
代码的可重用性差:由于是面向结构的编程方式,并且混合html,所以可能页面原型修改一点,整个程序都需要修改,更别提代码重用了。
老尸 该用户已被删除
8#
发表于 2015-3-17 19:08:19 | 只看该作者
ASP.net的服务器,要求安装一个.net环境,当然我这里指的是windows系统,顺便点一下,.net只能放在windows环境里来运行。Asp.net1.1的就装Framework1.1,Asp.net2.0的就装Framework2.0。
深爱那片海 该用户已被删除
9#
发表于 2015-3-24 21:05:29 | 只看该作者
那么,ASP.Net有哪些改进呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-3 17:07

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表