繁体   English   中英

如何在原型单元格上使用UITableView单元格及其元素

[英]How do I use a UITableView cell and its elements on prototype cell

我已经使用此Master-Detail应用程序启动了一个新的iPad项目。 我不是使用Interface Builder创建东西的忠实拥护者,但是对于这个项目,特别是我必须使用它。

我需要创建一个自定义单元格以在MasterViewController上使用。 该单元格将包含一个开关和一个标签。 使用界面构建器,我将这些元素拖到了称为TableView Prototype Content的内部的Prototype Cells中。

这就是我得到的。

在此处输入图片说明

现在如何在tableView:cellForRowIndexPath:使用它? 我必须要有商店才能使用该UILabelUISwitch吗? 我怎么做?

谢谢

是。 您应该创建一个UITableViewCell子类,并为需要访问的每个控件添加IBOutlets 然后,将您的子类设置为身份检查器中每个原型单元的自定义类。

您还需要设置重用标识符。 然后,您可以像在tableView:cellForRowIndexPath:通常那样使单元出队:

YourCustomCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

您需要在Interface Builder中为单元格提供一个Reuse Identifier ,然后您可以通过以下方式访问该单元格:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

无需子类化,您可以为单元格内的每个元素赋予标签并使用:

UISwitch *checkSwitch = (UISwitch *)[cell viewWithTag:-1];

首先创建UITableViewCell子类,并将其设置为Storyboard中的Prototype Cell。 其次,在Storyboard中为Prototype单元提供一个重用标识符。 在新创建的UITableViewCell子类中创建UILabelUISwitch IBOutlet将自定义单元格类导入到viewcontroller.m文件中在cellForRowAtIndexPath:方法使自定义单元格出队

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 static NSString *cellID = @"cell"; // identifier you have entered in storyboards
 CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

 if(!cell)
 {
  cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
 }

 //access your custom cell properties from now on
 cell.mySwith // or whatever name you gave it in your outlet

 return cell;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM