iphoneuitableviewtextlabel

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.


Solution

  • Use the UILabel lineBreakMode property to restrict your text within the width of your UILabel

    @property(nonatomic) UILineBreakMode lineBreakMode
    

    Use it as below.

    myLabel.lineBreakMode = UILineBreakModeTailTruncation;
    

    Here is the list of values which could be used with lineBreakMode.

    http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/doc/c_ref/UILineBreakMode

    EDITED:

    Set the width of your UILabel as per your requirment

    eg.

    myLabel.frame.size.width = 320;