简体   繁体   English

UITableViewCell 附件视图不显示

[英]UITableViewCell accessoryView not displaying

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.谢谢。

cell.accessoryView = label;

You are setting your accessoryView to be a label so you're not going to see any disclosure indicator.您将您的附件视图设置为 label,因此您不会看到任何披露指示符。 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...要了解不同的内置表 styles,请查看下图...

在此处输入图像描述

You can adjust the textLabel and detailTextLabel to suit your taste.您可以调整 textLabel 和 detailTextLabel 以适合您的口味。

Why this?为什么这个? You can't have both.你不能两者兼得。

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
-(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 UITableViewCellStyleValue1 or UITableViewCellStyleValue2 and set the textLabel and detailTextLabel properties as you need.传入UITableViewCellStyleValue1UITableViewCellStyleValue2并根据需要设置textLabeldetailTextLabel属性。

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

UITableViewCellStyleValue2 gives me a weird style. UITableViewCellStyleValue2 给了我一种奇怪的风格。 I think the most commonly requested style is UITableViewCellStyleValue1 (at least for my case).我认为最常要求的样式是 UITableViewCellStyleValue1 (至少对我而言)。

Also remember: accessoryType property is ignored if you have custom accessoryView.还要记住:如果你有自定义的附件视图,附件类型属性将被忽略。 So, this line of code is excess.所以,这行代码是多余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM