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

有任何想法吗?谢谢。

4

5 回答 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 以适合您的口味。

于 2011-06-14T13:40:02.583 回答
6

为什么这个?你不能两者兼得。

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
于 2011-06-14T13:23:50.413 回答
3
-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier

在 iOS 3.0 中被贬值

而是使用:

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

传入UITableViewCellStyleValue1orUITableViewCellStyleValue2并根据需要设置textLabelanddetailTextLabel属性。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html%23//apple_ref/doc/uid/TP40006938-CH3-SW34

于 2011-06-14T13:32:47.977 回答
0

UITableViewCellStyleValue2 给了我一种奇怪的风格。我认为最常要求的样式是 UITableViewCellStyleValue1 (至少对我而言)。

于 2015-04-19T11:12:48.540 回答
0

还要记住:如果你有自定义的附件视图,附件类型属性将被忽略。所以,这行代码是多余的。

于 2017-06-08T07:46:48.713 回答