Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uitableviewcell textlabel too long and push detailtextlabel out of view

When i'm using UITableViewCellStyleValue1, i got a long string of textLabel, and somehow the detailTextLabel got push out from the view.

When i shorted my textLabel text, then i can see the detailTextLabel's text.

Is there anyway to limit the width of textLabel in the above style so that it will truncate the textLabel with it's too long?

My code is:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  static NSString *CellIdentifier = @"Cell";  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) {     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];  }  cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;  //---get the letter in each section; e.g., A, B, C, etc.--- NSString *alphabet = [self.currencyNameIndex objectAtIndex:[indexPath section]];  //---get all states beginning with the letter--- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; self.currencyList = [self.keyCurrencyName filteredArrayUsingPredicate:predicate];  if ([self.currencyList count] > 0)  {     NSString *currencyName = [self.keyCurrencyName objectAtIndex:indexPath.row];     cell.textLabel.text = currencyName;      NSString *currencyCode = [self.valueCurrencyCode objectAtIndex:indexPath.row];     cell.detailTextLabel.text = currencyCode;  }     return cell; } 

so my currency name will be a long one on some entry.

like image 305
lucas Avatar asked Apr 21 '11 15:04

lucas


2 Answers

Simplest for me was to subclass UITableViewCell and override the layoutSubviews.

Couldn't find a reliable way to calculate the positions from just the label frames so just hardcoded the accessory width for in this case a UITableViewCellStyleValue1 cell with a UITableViewCellAccessoryDisclosureIndicator accessory type.

- (void)layoutSubviews {     [super layoutSubviews];      CGFloat detailTextLabelWidth = [self.detailTextLabel.text sizeWithFont:self.detailTextLabel.font].width;     CGRect detailTextLabelFrame = self.detailTextLabel.frame;      if (detailTextLabelFrame.size.width <= detailTextLabelWidth && detailTextLabelWidth > 0) {         detailTextLabelFrame.size.width = detailTextLabelWidth;         CGFloat accessoryWidth = (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) ? 28.0f : 35.0f;         detailTextLabelFrame.origin.x = self.frame.size.width - accessoryWidth - detailTextLabelWidth;         self.detailTextLabel.frame = detailTextLabelFrame;          CGRect textLabelFrame = self.textLabel.frame;         textLabelFrame.size.width = detailTextLabelFrame.origin.x - textLabelFrame.origin.x;         self.textLabel.frame = textLabelFrame;     } } 
like image 139
Gosoftworks Development Avatar answered Oct 22 '22 04:10

Gosoftworks Development


@Jhaliya @lucas

cell.textLabel.numberOfLines = 3; // set the numberOfLines cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation; 

see here: Custom UITableViewCell. Failed to apply UILineBreakModeTailTruncation

like image 34
c0ming Avatar answered Oct 22 '22 02:10

c0ming