Saturday, June 8, 2013

UITableViewCell UITableViewCellStyleValue1 example in Objective C (iOS).


UITableViewCell UITableViewCellStyleValue1

Cell Styles
An enumeration for the various styles of cells.

typedef enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
} UITableViewCellStyle;

Constants
UITableViewCellStyleDefault
A simple style for a cell with a text label (black and left-aligned) and an optional image view. Note that this is the default style for cells prior to iOS 3.0.
UITableViewCellStyleValue1
A style for a cell with a label on the left side of the cell with left-aligned and black text; on the right side is a label that has smaller blue text and is right-aligned. The Settings application uses cells in this style.
UITableViewCellStyleValue2
A style for a cell with a label on the left side of the cell with text that is right-aligned and blue; on the right side of the cell is another label with smaller text that is left-aligned and black. The Phone/Contacts application uses cells in this style.
UITableViewCellStyleSubtitle
A style for a cell with a left-aligned label across the top and a left-aligned label below it in smaller gray text. The iPod application uses cells in this style.

Discussion of [UITableViewCell UITableViewCellStyleValue1]
In all these cell styles, the larger of the text labels is accessed via the textLabel property and the smaller via the detailTextLabel property.

UITableViewCell UITableViewCellStyleValue1 example.
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

}

cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;

Example of [UITableViewCell UITableViewCellStyleValue1].
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
            }
            NSDictionary *item = [tableData objectAtIndex:(indexRow-cellShift)];
            NSString *name = [item objectForKey:@"Name"];
            if ([name length] > MaxVendorsLength ) {
                name =  [NSString stringWithFormat:@"%@ ...",[name substringToIndex:MaxVendorsLength]];
            }
            cell.textLabel.text = name;
            cell.textLabel.minimumFontSize = 12;

UITableViewCell UITableViewCellStyleValue1 example.
    static NSString *CellIdentifier = @"Cell";

    UILabel *label;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
    }

    label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 280, 34)];

    [label setNumberOfLines:2];

    label.backgroundColor = [UIColor clearColor];

    [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];

    label.adjustsFontSizeToFitWidth = NO;

    [[cell contentView] addSubview:label];

End of UITableViewCell UITableViewCellStyleValue1 example article.