xcode 来自自定义 .XIB 文件的 UITableViewCell 不会创建插座

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9921241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:49:31  来源:igfitidea点击:

UITableViewCell from a custom .XIB file doesn't create outlets

iphoneobjective-ciosxcodeuitableview

提问by Argent

Update 2:

更新 2:



Long story short, I was being silly (and, in my defense, not properly educated) about this. It works now. My questions have derailed from the original topic a little bit, but that's because I wasn't understanding what was going on in my application. I would like to close the question with one last (and small) query:

长话短说,我在这方面很愚蠢(并且,在我的辩护中,没有受到适当的教育)。它现在有效。我的问题有点偏离了最初的主题,但那是因为我不了解我的申请中发生了什么。我想用最后一个(和小的)查询来结束这个问题:

I've got two labels in my customCell.xib. I want one of them (cell.label2) to sometimes contain a longer segment of text (2-3 lines). I know one way to make it all fit is to set the autoshrink property, but that shrinks to the text so it can fit on a single line. I want to preserve the original text size and expand the cell's height instead, making the text span multiple lines instead of shrinking it.

我的 customCell.xib 中有两个标签。我希望其中一个 ( cell.label2) 有时包含更长的文本段(2-3 行)。我知道让所有内容都适合的一种方法是设置 autoshrink 属性,但这会缩小到文本,以便它可以放在一行中。我想保留原始文本大小并扩展单元格的高度,使文本跨越多行而不是缩小它。

Is there a way to do this?

有没有办法做到这一点?

Update 1:

更新 1:



I tried a few things based on the replies below, and they got me nowhere. I am growing convinced that I am doing something fundamentally wrong, and you guys just can't think of it because it's so basic. So let's try again. I am going to changed the names a little, so they are easier to remember.

我根据下面的回复尝试了一些东西,但他们一无所获。我越来越相信我在做一些根本错误的事情,你们只是想不出来,因为它太基本了。所以让我们再试一次。我将稍微更改名称,以便更容易记住。

The problem seems to be in the fact that I can't create any IBOutletsor IBActionsfor my customCell. Right now I have 3 files that should handle this (DetailedDirectionViewCell.{h,m,xib}, but the Interface Builder doesn't allow me to create a property/outlet/reference out of my UITableViewCellobject - anywhere.

问题似乎在于我无法为我的 customCell创建任何IBOutletsIBActions。现在我有 3 个文件应该处理这个问题(DetailedDirectionViewCell.{h,m,xib},但接口生成器不允许我从我的UITableViewCell对象中创建属性/出口/引用- 任何地方。

Instead of copying the code here, I've provided a PasteBin entry with links to my code. As before, I've removed the less interesting methods. Take a look if you will. http://pastebin.com/p4eADhKQ

我没有在此处复制代码,而是提供了一个 PasteBin 条目,其中包含指向我的代码的链接。和以前一样,我删除了不太有趣的方法。如果你愿意的话就看看吧。 http://pastebin.com/p4eADhKQ

I also have customCell.{h,m}, but those are just new Objective C class files that inherit from UITableViewCell. customCell.xibis just a cell with two labels.

我也有 customCell.{h,m},但这些只是从UITableViewCell. customCell.xib只是一个带有两个标签的单元格。



So I have a couple of problems really.

所以我真的有几个问题。

First, generating a UITableViewprogrammatically, using a custom UITableViewCellcontained in a .XIB file of its own. My DirectionsViewController class is just a UITableViewwith programmatic cells. Tapping on one of the cells needs to present a DetailedDirectionsViewController table (in a modal way), the cell design for which sits in a DetailedDirectionsViewCell.xib file. The problem is, I can't create an IBOutletfor the UITableViewCellfrom the nib file - anywhere. Dragging the File's Owner icon/outlet doesn't offer to create anything. Which, after 5 hours of struggling, means that I can't present my detailed view.

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

The second problem involves adding a navigation bar to the DirectionsViewController, but let's leave that alone for now.

第二个问题涉及向 DirectionsViewController 添加导航栏,但我们暂时先不管它。

Here are some methods you might find helpful:

以下是一些您可能会觉得有用的方法:

//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;
}

I don't think the rest of the methods are of interest, because they are either working as expected, or are pretty much the default ones.

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

To sum up:

总结:

DirectionsViewController - essentially a UITableViewControllerwith custom cells. No .xib file DetailedDirectionsViewController - detailed information about the entries from DirectionsViewController. Cells here should come from a .XIB file, but that's broken. DetailedDirectionsViewCell - this is the custom cell. I can't set its File's Owner.

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



回答by rohan-patel

Ok.. You do not create IBOutletconnection from File's Owner. Have a look at a screenshot. You create IBOutletfrom CustomCell's view(with Red Arrow).

好的.. 您没有IBOutlet从文件所有者创建连接。看一下截图。您IBOutlet从 CustomCell 的视图(带有红色箭头)创建。

Looking after your code just follow these steps.

照看您的代码只需按照以下步骤操作即可。

1) Goto CustomCell.hfile. As you are saying customCell.xibhas two UILabels(assume label1 & label2) you gonna have to declare properties and create outlets in CustomCell.hfile and in .m file synthesize and release it. Refer this code screen of mine.

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

enter image description here

在此处输入图片说明

2) Now in CustomCell.xib, select view of CustomCellnot File's Owner(File's Owner should inherit from NSObjectonly)go to Identity Inspector(Marked with Red Ellipse) and select the corresponding Customcell class (marked with Red rectangle).

2) 现在在 中CustomCell.xib选择CustomCell非文件所有者的视图(文件所有者应该NSObject只继承自)转到身份检查器(用红色椭圆标记)并选择相应的 Customcell 类(用红色矩形标记)。

3) Right click your customcell's view and make connections to labels. And save it..

3) 右键单击​​自定义单元格的视图并连接到标签。并保存它..

enter image description here

在此处输入图片说明

4) In your DirectionsViewController.myou have this UITableView's delegate method cellForRowAtIndexPath. Change it like this :

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;

}

this delegate method is gonna be called as many times as you returned value in numberOfRowsInSectionDataSource method. Every time cell.label will be blank and you will add data to label by calling this line. so no need to create label each time as you did between line 79-90 here. http://pastebin.com/Vd4PKjTu

这个委托方法的调用次数与您在numberOfRowsInSectionDataSource 方法中返回的值一样多。每次 cell.label 将为空白时,您将通过调用此行将数据添加到标签。因此无需像此处的第 79-90 行那样每次都创建标签。http://pastebin.com/Vd4PKjTu

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

Creating custom cell means you create UI(i.e. .xib),interface & implementation (.h,.m file) for UITableViewCellby yourself and adopt them in your class's (i.e. DirectionsViewController.m) cellForRowAtIndexPathdelegate method.

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

回答by Scar

To load a custom cell, I've used this code (test it and it's working)

要加载自定义单元格,我使用了此代码(测试它并且它正在工作)

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;
             }
        }
}

and make sure that you changed the cell class nib file as the following:

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

  1. Click on the files owner, change the type of it to NSObject(this for not to confuse when connect the Outlets.
  2. Remove any view you have in the nib file, then, drag and drop a UITableViewCellcomponent.
  3. Change the super class of the component to the Cell class, in your case, change the cell from UITableViewCellto DetailedDirectionsViewCell.
  4. Connect the outlet.
  1. 单击文件所有者,将其类型更改为NSObject(这是为了在连接 Outlets 时不要混淆。
  2. 删除您在 nib 文件中的任何视图,然后拖放UITableViewCell组件。
  3. 将组件的超类更改为 Cell 类,在您的情况下,将单元格从 更改UITableViewCellDetailedDirectionsViewCell
  4. 连接插座。

This should work, let me know if you have any question.

这应该有效,如果您有任何问题,请告诉我。

回答by GeneralMike

To answer your last question in Update 2, did you consider using a text field instead of a label? You should be able to disable editing so the user can't change what is displayed. If you need it to be a label, in the Attributes Inspector for the label, there is a property called "Lines" under the "Label" drop down that you can adjust. I'm not sure how to access that property programmaticly, but a quick Google search should help you out there.

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

回答by Nikolay Shubenkov

In my case I had two different views for one cell class. And when I connected outlets to .h file they were connected only to the first view representation, not the second. So to ensure that your outlets are really connected go Xcode me menu and open View -> Utilities -> Show FileConnection inspector, then make sure that your view's outlets are really connected.

就我而言,我对一个细胞类有两种不同的看法。当我将插座连接到 .h 文件时,它们只连接到第一个视图表示,而不是第二个。所以为了确保你的插座真的连接到 Xcode me 菜单并打开 View -> Utilities -> Show FileConnection inspector,然后确保你的视图的 outlet 真的连接了。

enter image description here

在此处输入图片说明