iphone – 来自自定义.XIB文件的UITableViewCell不会创建出口
更新2:
长话短说,我对此很愚蠢(而且,在我的辩护中,没有得到适当的教育).它现在有效.我的问题已经脱离了原来的主题一点点,但那是因为我不理解我的应用程序中发生了什么.我想用最后一个(和小的)查询来关闭这个问题: 我的customCell.xib中有两个标签.我想要其中一个(cell.label2)有时包含更长的文本段(2-3行).我知道一种使它完全适合的方法是设置自动收缩属性,但缩小到文本,以便它可以适合单行.我想保留原始文本大小并扩展单元格的高度,使文本跨越多行而不是缩小它. 有没有办法做到这一点? 更新1: 我根据下面的回复尝试了一些事情,他们让我无处可去.我越来越相信我做的事情根本就是错误的,而你们只是想不到它,因为它是如此基本.所以让我们再试一次.我要稍微更改名称,以便更容易记住. 问题似乎在于我无法为customCell创建任何IBOutlets或IBActions.现在我有3个文件应该处理这个(DetailedDirectionViewCell.{h,m,xib},但是Interface Builder不允许我在任何地方创建我的UITableViewCell对象的属性/出口/引用. 我没有在这里复制代码,而是提供了一个带有我代码链接的PasteBin条目.和以前一样,我删除了不那么有趣的方法.看看你会不会. 我也有customCell.{h,m},但这些只是继承自UITableViewCell的新Objective C类文件. customCell.xib只是一个带有两个标签的单元格. 所以我真的有几个问题. 首先,使用自己的.XIB文件中包含的自定义UITableViewCell以编程方式生成UITableView.我的DirectionsViewController类只是一个带有编程单元的UITableView.点击其中一个单元格需要提供DetailedDirectionsViewController表(以模态方式),其单元格设计位于DetailedDirectionsViewCell.xib文件中.问题是,我无法从nib文件中为UITableViewCell创建一个IBOutlet.拖动文件的所有者图标/插座不提供任何创建.经过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文件 解决方法
好的..您没有从文件所有者创建IBOutlet连接.看一下截图.您可以从CustomCell的视图(使用红色箭头)创建IBOutlet.
照顾您的代码,请按照以下步骤操作. 1)转到CustomCell.h文件.正如您所说,customCell.xib有两个UILabel(假设label1和amp; label2),您必须在CustomCell.h文件中声明属性并创建出口,并在.m文件中合成并释放它.请参阅我的此代码屏幕. 2)现在在CustomCell.xib中,选择CustomCell的视图而不是File的所有者(文件的所有者应该从NSObjectonly继承)转到Identity Inspector(用红色椭圆标记)并选择相应的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行之间那样创建标签. http://pastebin.com/Vd4PKjTu cell.label1.text=[someArray ObjectAtIndexPath: indexPath.row]; 创建自定义单元格意味着您创建UI(即.xib),界面& UITableViewCell的实现(.h,.m文件)由你自己实现,并在你的类(即DirectionsViewController.m)cellForRowAtIndexPath委托方法中采用它们. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |