简体   繁体   English

来自自定义.XIB文件的UITableViewCell不会创建出口

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

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. 我的customCell.xib中有两个标签。 I want one of them ( cell.label2 ) to sometimes contain a longer segment of text (2-3 lines). 我想要其中一个( cell.label2 )有时包含更长的文本段(2-3行)。 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. 我想保留原始文本大小并扩展单元格的高度,使文本跨越多行而不是缩小它。

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 IBOutlets or IBActions for my customCell. 问题似乎在于我无法为customCell创建任何IBOutletsIBActions 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 UITableViewCell object - anywhere. 现在我有3个文件应该处理这个(DetailedDirectionViewCell。{h,m,xib},但是Interface Builder不允许我在任何地方创建我的UITableViewCell对象的属性/出口/引用。

Instead of copying the code here, I've provided a PasteBin entry with links to my code. 我没有在这里复制代码,而是提供了一个带有我代码链接的PasteBin条目。 As before, I've removed the less interesting methods. 和以前一样,我删除了不那么有趣的方法。 Take a look if you will. 看看你会不会。 http://pastebin.com/p4eADhKQ http://pastebin.com/p4eADhKQ

I also have customCell.{h,m}, but those are just new Objective C class files that inherit from UITableViewCell . 我也有customCell。{h,m},但这些只是继承自UITableViewCell新Objective C类文件。 customCell.xib is just a cell with two labels. customCell.xib只是一个带有两个标签的单元格。


So I have a couple of problems really. 所以我真的有几个问题。

First, generating a UITableView programmatically, using a custom UITableViewCell contained in a .XIB file of its own. 首先,使用自己的.XIB文件中包含的自定义UITableViewCell以编程方式生成UITableView My DirectionsViewController class is just a UITableView with programmatic cells. 我的DirectionsViewController类只是一个带有编程单元的UITableView 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. 点击其中一个单元格需要提供DetailedDirectionsViewController表(以模态方式),其单元格设计位于DetailedDirectionsViewCell.xib文件中。 The problem is, I can't create an IBOutlet for the UITableViewCell from the nib file - anywhere. 问题是,我无法从nib文件中为UITableViewCell创建一个IBOutlet 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. 经过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 UITableViewController with custom cells. DirectionsViewController - 本质上是一个带有自定义单元格的UITableViewController No .xib file DetailedDirectionsViewController - detailed information about the entries from DirectionsViewController. No .xib文件DetailedDirectionsViewController - 有关DirectionsViewController条目的详细信息。 Cells here should come from a .XIB file, but that's broken. 这里的单元格应该来自.XIB文件,但是它已经坏了。 DetailedDirectionsViewCell - this is the custom cell. DetailedDirectionsViewCell - 这是自定义单元格。 I can't set its File's Owner. 我无法设置其文件所有者。


Ok.. You do not create IBOutlet connection from File's Owner. 好的..您没有从文件所有者创建IBOutlet连接。 Have a look at a screenshot. 看一下截图。 You create IBOutlet from CustomCell's view(with Red Arrow). 您可以从CustomCell的视图(使用红色箭头)创建IBOutlet

Looking after your code just follow these steps. 照顾您的代码,请按照以下步骤操作。

1) Goto CustomCell.h file. 1)转到CustomCell.h文件。 As you are saying customCell.xib has two UILabel s(assume label1 & label2 ) you gonna have to declare properties and create outlets in CustomCell.h file and in .m file synthesize and release it. 正如您所说customCell.xib有两个UILabel (假设label1 & label2 ),您必须在CustomCell.h文件中声明属性并创建出口,并在.m文件中合成并释放它。 Refer this code screen of mine. 请参阅我的此代码屏幕。

在此输入图像描述

2) Now in CustomCell.xib , select view of CustomCell not File's Owner(File's Owner should inherit from NSObject only) 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. 3)右键单击customcell的视图并连接标签。 And save it.. 并保存..

在此输入图像描述

4) In your DirectionsViewController.m you have this UITableView's delegate method cellForRowAtIndexPath . 4)在DirectionsViewController.m你有这个UITableView的委托方法cellForRowAtIndexPath Change it like this : 像这样改变它:

 - (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 numberOfRowsInSection DataSource method. 当您在numberOfRowsInSection DataSource方法中返回值时,将调用此委托方法的次数。 Every time cell.label will be blank and you will add data to label by calling this line. 每次cell.label为空,您将通过调用此行将数据添加到标签。 so no need to create label each time as you did between line 79-90 here. 因此,无需像每次在第79-90行之间那样创建标签。 http://pastebin.com/Vd4PKjTu http://pastebin.com/Vd4PKjTu

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

Creating custom cell means you create UI(ie .xib),interface & implementation (.h,.m file) for UITableViewCell by yourself and adopt them in your class's (ie DirectionsViewController.m ) cellForRowAtIndexPath delegate method. 创建自定义单元格意味着您自己为UITableViewCell创建UI(即.xib),界面和实现(.h,.m文件),并在您的类(即DirectionsViewController.mcellForRowAtIndexPath委托方法中采用它们。

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. 单击文件所有者,将其类型更改为NSObject (这是为了在连接Outlets时不要混淆。
  2. Remove any view you have in the nib file, then, drag and drop a UITableViewCell component. 删除nib文件中的所有视图,然后拖放UITableViewCell组件。
  3. Change the super class of the component to the Cell class, in your case, change the cell from UITableViewCell to DetailedDirectionsViewCell . 将组件的超类更改为Cell类,在您的情况下,将单元格从UITableViewCell更改为DetailedDirectionsViewCell
  4. Connect the outlet. 连接插座。

This should work, let me know if you have any question. 这应该有用,如果您有任何疑问,请告诉我。

To answer your last question in Update 2, did you consider using a text field instead of a label? 要回答更新2中的最后一个问题,您是否考虑使用文本字段而不是标签? 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. 我不确定如何以程序方式访问该属性,但快速的Google搜索应该可以帮助您。

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. 当我将插座连接到.h文件时,它们只连接到第一个视图表示,而不是第二个。 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. 因此,要确保您的插座真正连接,请转到Xcode me菜单并打开View - > Utilities - > Show FileConnection检查器,然后确保您的视图插座真正连接。

在此输入图像描述

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

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