ios UITableViewCell 附件视图不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6344105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:21:37  来源:igfitidea点击:

UITableViewCell accessoryView not displaying

iosuitableviewcocoa-touchaccessoryview

提问by Andy A

Im totally unsure why my accessory view is not working. I simply want some text to appear to the right of the UITableViewCell (as well as the left), but only the text on the left is displaying.

我完全不确定为什么我的附件视图不起作用。我只是希望一些文本出现在 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;
}

Any ideas? Thanks.

有任何想法吗?谢谢。

回答by Kirby Todd

cell.accessoryView = label;

You are setting your accessoryView to be a label so you're not going to see any disclosure indicator. If you want title and detail text in your cell then init it with UITableViewCellStyleValue2 like this...

您将附件视图设置为标签,因此您不会看到任何披露指示符。如果你想在你的单元格中添加标题和细节文本,那么像这样用 UITableViewCellStyleValue2 初始化它......

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

...and then set the title and detail like this...

...然后像这样设置标题和详细信息...

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

To get an idea of the different built in table styles check the following image...

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

enter image description here

在此处输入图片说明

You can adjust the textLabel and detailTextLabel to suit your taste.

您可以调整 textLabel 和 detailTextLabel 以适合您的口味。

回答by LuckyLuke

Why this? You can't have both.

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

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

回答by Carlton Gibson

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

was depreciated in iOS 3.0

在 iOS 3.0 中被折旧

Instead use:

而是使用:

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

Pass in either UITableViewCellStyleValue1or UITableViewCellStyleValue2and set the textLabeland detailTextLabelproperties as you need.

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

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

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

回答by emersonku

UITableViewCellStyleValue2 gives me a weird style. I think the most commonly requested style is UITableViewCellStyleValue1 (at least for my case).

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

回答by Miroslav

Also remember: accessoryType property is ignored if you have custom accessoryView. So, this line of code is excess.

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