Iphone 自定义.XIB文件中的UITableViewCell不';不要创造出口
更新2:Iphone 自定义.XIB文件中的UITableViewCell不';不要创造出口,iphone,objective-c,ios,xcode,uitableview,Iphone,Objective C,Ios,Xcode,Uitableview,更新2: 长话短说,我在这件事上很傻(而且,为我辩护,我没有受过适当的教育)。现在可以了。我的问题有点偏离了原来的主题,但那是因为我不了解我的应用程序中发生了什么。我想用最后一个(也是很小的)问题来结束这个问题: 我的customCell.xib中有两个标签。我希望其中一个(cell.label2)有时包含更长的文本段(2-3行)。我知道一种方法是设置autoshrink属性,但该属性会收缩到文本,因此可以在一行上进行调整。我希望保留原始文本大小并扩展单元格的高度,使文本跨越多行而不是收缩 有
长话短说,我在这件事上很傻(而且,为我辩护,我没有受过适当的教育)。现在可以了。我的问题有点偏离了原来的主题,但那是因为我不了解我的应用程序中发生了什么。我想用最后一个(也是很小的)问题来结束这个问题: 我的customCell.xib中有两个标签。我希望其中一个(
cell.label2
)有时包含更长的文本段(2-3行)。我知道一种方法是设置autoshrink属性,但该属性会收缩到文本,因此可以在一行上进行调整。我希望保留原始文本大小并扩展单元格的高度,使文本跨越多行而不是收缩
有办法做到这一点吗
更新1:
根据下面的回复,我尝试了一些方法,但结果一无所获。我越来越确信我在做一些根本错误的事情,你们这些家伙就是想不起来,因为这太基本了。让我们再试一次。我打算把名字改一下,这样更容易记住 问题似乎在于,我无法为我的customCell创建任何
IBOutlets
或IBActions
。现在我有3个文件可以处理这个(DetailedDirectionViewCell.{h,m,xib}),但是界面生成器不允许我在任何地方创建我的UITableViewCell
对象的属性/出口/引用
我没有在这里复制代码,而是提供了一个带有代码链接的PasteBin条目。与以前一样,我删除了不太有趣的方法。如果愿意,请看一看。
我还有customCell.{h,m},但这些只是继承自UITableViewCell
customCell.xib
的新的目标C类文件,只是一个有两个标签的单元格
所以我真的有几个问题 首先,使用.XIB文件中包含的自定义
UITableViewCell
以编程方式生成一个UITableView
。My DirectionsViewController类只是一个带有编程单元格的UITableView
。点击其中一个单元格需要(以模式方式)显示一个详细的DirectionsViewController表,其单元格设计位于DetailedDirectionsViewCell.xib文件中。问题是,我无法从nib文件-anywhere中为UITableViewCell
创建IBOutlet
。拖动文件的所有者图标/outlet无法创建任何内容。经过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;
}
我认为其余的方法不值得关注,因为它们要么按预期工作,要么基本上是默认的
总而言之:
DirectionViewController-本质上是一个带有自定义单元格的UITableViewController
。No.xib文件
DetailedDirectionsViewController-有关DirectionsViewController.Cells中条目的详细信息,此处的单元格应来自.XIB文件,但该文件已损坏。
DetailedDirectionsViewCell-这是自定义单元格。我无法设置其文件的所有者
要加载自定义单元格,我使用了以下代码(测试它,它就可以工作了) 并确保已按以下方式更改了单元类nib文件:
NSObject
(这是为了在连接插座时不会混淆)UITableViewCell
组件UITableViewCell
更改为DetailedDirectionsViewCell
这应该可以,如果您有任何问题,请告诉我。好的。您不需要从文件所有者创建
IBOutlet
连接。请查看屏幕截图。您可以从CustomCell的视图(带红色箭头)创建IBOutlet
只需按照以下步骤操作代码
1) 转到CustomCell.h
文件。正如您所说的CustomCell.xib
有两个UILabel
s(假设label1和label2
),您必须在CustomCell.h
文件和.m文件中声明属性并创建出口。请参考我的这个代码屏幕
2) 现在在CustomCell.xib
中,选择查看CustomCell
而不是文件的所有者(文件的所有者应仅从NSObject
继承)转到Identity Inspector(用红色椭圆标记)并选择相应的CustomCell类(用红色矩形标记)
3) 在customcell的视图上单击鼠标右键,并与标签建立连接。然后保存它
4) 在您的DirectionsViewController.m
中,有此UITableView的委托方法cellforrowatinexpath
。更改如下:
- (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;
}
调用此委托方法的次数与在numberofrowsinssection
DataSource方法中返回值的次数相同。每次cell.label都将为空,您将通过调用此行向标签添加数据。因此,无需像在第79-90行之间那样每次都创建标签
创建自定义单元格意味着您自己为
UITableViewCell
创建UI(即.xib)、接口和实现(.h、.m文件),并在类中采用它们(即DirectionsViewController.m
)cellforrowatinexpath
委托方法。要回答更新2中的最后一个问题,您是否考虑使用文本字段而不是标签?您应该能够禁用编辑,以便用户无法更改显示内容。如果你需要一个l
- (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;
}
cell.label1.text=[someArray ObjectAtIndexPath: indexPath.row];