从自定义.XIB文件的UITableViewCell不会创buildsockets

更新2:


长话短说,我是愚蠢的(而且,在我的防守,没有适当的教育)这个。 它现在有效。 我的问题已经脱离了原来的主题,但是那是因为我不了解我的应用程序正在发生什么。 我想用最后一个(和小的)查询来closures这个问题:

我的customCell.xib中有两个标签。 我希望其中的一个( cell.label2 )有时包含更长的文本段(2-3行)。 我知道一个办法是让自动缩放属性设置为合适的,但缩小到文本的位置,这样就可以放在一条线上。 我想保留原始的文本大小,并扩大单元格的高度,使文本跨越多行而不是缩小它。

有没有办法做到这一点?

更新1:


我根据下面的回复尝试了一些东西,他们让我无处可去。 我越来越相信我正在做一些根本性的错误,你们不能想到它,因为它是如此基本。 所以我们再试一次。 我会稍微改变一下名字,所以他们更容易记住。

问题似乎是因为我无法为我的customCell创build任何IBOutletsIBActions 。 现在我有3个文件应该处理这个(DetailedDirectionViewCell。{h,m,xib},但是Interface Builder不允许我在任何地方从我的UITableViewCell对象中创build一个属性/ outlet / reference。

而不是在这里复制代码,我提供了一个PasteBin条目,并带有指向我的代码的链接。 和以前一样,我删除了不太有趣的方法。 看看你是否愿意。 http://pastebin.com/p4eADhKQ

我也有customCell {h,m},但这些只是从UITableViewCellinheritance的新的Objective C类文件。 customCell.xib只是一个有两个标签的单元格。


所以我真的有几个问题。

首先,使用自己的.XIB文件中包含的自定义UITableViewCell以编程方式生成UITableView 。 我的DirectionsViewController类只是一个带有编程单元的UITableView 。 点击其中一个单元格需要显示DetailedDirectionsViewController表格(以模态方式),其细胞devise位于DetailedDirectionsViewCell.xib文件中。 问题是,我无法从nib文件创buildUITableViewCellIBOutlet – 在任何地方。 拖动文件的所有者图标/sockets不提供创build任何东西。 经过5个小时的挣扎,意味着我不能提出我的详细观点。

第二个问题涉及在DirectionsViewController中添加一个导航栏,但现在让我们单独离开。

以下是一些您可能会发现有帮助的方法:

 //inside DirectionsViewController - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) { DetailedDirectionsViewController *vc = [[DetailedDirectionsViewController alloc] initWithStyle:UITableViewStyleGrouped]; vc.instruction = [turnList objectAtIndexPath:indexPath.row]; [self.tabBarController presentModalViewController:vc animated:YES]; } //inside DetailedDirectionsViewController - (UITableViewCell *) tableView:(UITableView *) cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"directionsCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { [[NSBundle mainBundle] initWIthNibNamed:@"DetailedDirectionsViewCell" owner:nil options:nil]; cell = self.tableViewCell; self.tableViewCell = nil; } //here I configure the custom cell return cell; } 

我不认为其余的方法是有趣的,因为他们要么按预期工作,要么是默认的。

总结一下:

DirectionsViewController – 本质上是一个具有自定义单元格的UITableViewController 。 没有.xib文件DetailedDirectionsViewController – 关于DirectionsViewController条目的详细信息。 这里的单元格应该来自.XIB文件,但是这是坏的。 DetailedDirectionsViewCell – 这是自定义单元格。 我无法设置文件的所有者。


好的..你不从文件的所有者创buildIBOutlet连接。 看看截图。 您从CustomCell的视图(使用红色箭头)创buildIBOutlet

照顾你的代码只需按照下列步骤。

1)转到CustomCell.h文件。 正如你所说customCell.xib有两个UILabel (假设label1 & label2 ),你必须声明属性并在CustomCell.h文件中创build出口,并在.m文件中合成并释放它。 参考我的这个代码屏幕。

在这里输入图像说明

2)现在在CustomCell.xibselect CustomCell.xib 视图而不是文件的所有者(文件的所有者应该只从NSObjectinheritance)转到Identity Inspector(用红色椭圆标记)并select相应的Customcell类(用红色矩形标记)。

3)右键单击您的customcell的视图,并连接到标签。 并保存它..

在这里输入图像说明

4)在你的DirectionsViewController.m你有这个UITableView的委托方法cellForRowAtIndexPath 。 像这样改变它:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CustomCellIdentifier = @"CustomCell"; EditProjectCustomCell *cell = (EditProjectCustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; // typecast to customcell [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EditProjectCustomCell" owner:self options:nil]; for (id oneObject in nib) if ([oneObject isKindOfClass:[EditProjectCustomCell class]]) cell = (EditProjectCustomCell *)oneObject; [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; } cell.label1.text=[someArray1 ObjectAtIndexPath: indexPath.row]; cell.label2.text=[someArray2 ObjectAtIndexPath: indexPath.row]; return cell; } 

这个委托方法会被调用多次,因为你在numberOfRowsInSection DataSource方法中返回值。 每次cell.label将是空白的,你会通过调用这一行添加数据标签。 所以不需要像这样在第79-90行之间每次创build标签。 http://pastebin.com/Vd4PKjTu

  cell.label1.text=[someArray ObjectAtIndexPath: indexPath.row]; 

创build自定义单元格意味着您自己为UITableViewCell创buildUI(即.xib),接口和实现(.h,.m文件),并在您的类(即DirectionsViewController.mcellForRowAtIndexPath委托方法中采用它们。

要加载自定义单元格,我已经使用了这个代码(testing它,它正在工作)

 static NSString *CustomCellIdentifier = @"CustomCommentIdentifier"; detailedDirectionsViewCell *cell = (DetailedDirectionsViewCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DetailedDirectionsViewCell" owner:self options:nil]; for (id oneObject in nib) { if ([oneObject isKindOfClass:[DetailedDirectionsViewCell class]]) { cell = (DetailedDirectionsViewCell *)oneObject; } } } 

并确保您更改了单元类nib文件,如下所示:

  1. 单击文件所有者,将其types更改为NSObject (这是为了在连接sockets时不会混淆。
  2. 删除nib文件中的所有视图,然后拖放一个UITableViewCell组件。
  3. 将组件的超类更改为Cell类,在您的情况下,将单元格从UITableViewCell更改为DetailedDirectionsViewCell
  4. 连接sockets。

这应该工作,让我知道如果你有任何问题。

要回答您在Update 2中的最后一个问题,您是否考虑使用文本字段而不是标签? 您应该能够禁用编辑,以便用户不能更改显示内容。 如果您需要将其作为标签,则在标签的“属性”检查器中,在“标签”下拉菜单中有一个名为“线条”的属性,您可以对其进行调整。 我不确定如何以编程方式访问该媒体资源,但快速的Googlesearch应该可以帮到你。

就我而言,我对一个单元类有两种不同的看法。 当我连接sockets.h文件时,他们只连接到第一个视图表示,而不是第二个。 所以要确保你的sockets是真正连接的Xcode我的菜单,并打开查看 – >实用程序 – >显示FileConnection督察,然后确保您的视图的sockets是真正连接。

在这里输入图像说明