UITableViewCell accessoryView未显示

8

我完全不确定为什么我的附件视图无法工作。我只想让一些文本出现在UITableViewCell的右侧(以及左侧),但是只有左侧的文本显示出来了。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];

  if (cell==nil){
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease];
      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 60, 30)];

      cell.textLabel.text = @"left text";
      label.text = @"right text";

      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

      cell.accessoryView = label;
      [label release];

  }
  return cell;
}

有任何想法吗?谢谢。

我建议您首先将标签对齐到右侧。默认情况下,标签对齐必须是左对齐。因此,两个文本重叠可能会导致问题。然后,您可以只需说:“[cell.contentView addSubView:label];” - Mikayil Abdullayev
5个回答

26
cell.accessoryView = label;

你将 accessoryView 设置为标签,因此不会看到任何披露指示器。如果您想在单元格中显示标题和详细文本,那么请使用 UITableViewCellStyleValue2 进行初始化,像这样...

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"cellType"] autorelease];

然后像这样设置标题和细节...

cell.textLabel.text = @"Title";
cell.detailTextLabel.text = @"Description";

为了了解不同的内置表格样式,请查看以下图片...

这里输入图片描述

您可以调整textLabel和detailTextLabel以适应您的喜好。


虽然这并没有直接回答我的问题(我在我的应用程序的其他地方使用UILabel作为accessoryView,所以我不确定为什么它在这里不起作用),但您确实为我提供了一些很好的替代方案。我主要关心的是文本被截断的问题。除非我使用自己的自定义单元格,否则没有其他方法吗? - Andy A
我得出结论,这些替代方案是正确的选择。 - Andy A

6

为什么会这样?你不能两全其美。

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

真的,那是我的错误。但即便如此,如果你删掉那一行代码,我的问题仍然存在。 - Andy A

3
-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier

iOS 3.0中已经弃用

请改用:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

传入UITableViewCellStyleValue1UITableViewCellStyleValue2,并根据需要设置textLabeldetailTextLabel属性。详细信息请参阅:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html%23//apple_ref/doc/uid/TP40006938-CH3-SW34

0

UITableViewCellStyleValue2 给我一种奇怪的样式。我认为最常请求的样式是 UITableViewCellStyleValue1(至少对于我的情况)。


0
还要记住:如果您有自定义的accessoryView,则会忽略accessoryType属性。因此,这行代码是多余的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接