8

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 *)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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Andy A
  • 4,191
  • 7
  • 38
  • 56
  • I suggest you give label alignment to right first. By default label alignment must be left. So two texts one on top of the other could be the problem. And then you can just say: [cell.contentView addSubView:label]; – Mikayil Abdullayev Jun 14 '11 at 13:25

5 Answers5

26
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...

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.

Kirby Todd
  • 11,254
  • 3
  • 32
  • 60
  • While this doesn't answer my question directly (I have a UILabel working as an accessoryView elsewhere in my app, so Im not sure why its not working here), you have certainly provided me with some great alternatives. The main concern I have, though, is that the text gets truncated. Is there no way around this (unless I use my own custom cell)? – Andy A Jun 14 '11 at 14:47
  • These alternatives, I have concluded, were the correct way to go. – Andy A Jun 22 '11 at 09:36
6

Why this? You can't have both.

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
  • True. My mistake. But nevertheless, my question still stands if you take out that line of code. – Andy A Jun 15 '11 at 08:47
3
-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier

was depreciated in 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.

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

Carlton Gibson
  • 7,278
  • 2
  • 36
  • 46
0

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

emersonku
  • 149
  • 1
  • 2
  • 8
0

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

Miroslav
  • 546
  • 2
  • 9
  • 22