Saturday, June 8, 2013

UITableViewCell initWithStyle example in Objective C (iOS).


UITableViewCell initWithStyle

Initializes a table cell with a style and a reuse identifier and returns it to the caller.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

Parameters of [UITableViewCell initWithStyle]
style
A constant indicating a cell style. See “Cell Styles” for descriptions of these constants.
reuseIdentifier
A string used to identify the cell object if it is to be reused for drawing multiple rows of a table view. Pass nil if the cell object is not to be reused. You should use the same reuse identifier for all cells of the same form.

Return Value of [UITableViewCell initWithStyle]
An initialized UITableViewCell object or nil if the object could not be created.

Discussion of [UITableViewCell initWithStyle]
This method is the designated initializer for the class. The reuse identifier is associated with those cells (rows) of a table view that have the same general configuration, minus cell content. In its implementation of tableView:cellForRowAtIndexPath:, the table view'€™s delegate calls the UITableView method dequeueReusableCellWithIdentifier:, passing in a reuse identifier, to obtain the cell object to use as the basis for the current row.[UITableViewCell initWithStyle]

If you want a table cell that has a configuration different that those defined by UITableViewCell for style, you must create your own custom cell. If you want to set the row height of cells on an individual basis, implement the delegate method tableView:heightForRowAtIndexPath:.

UITableViewCell initWithStyle example.
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
          cell = [[[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleSubtitle
             reuseIdentifier:CellIdentifier]
            autorelease];
 }

   cell.textLabel.text=[Array objectAtIndex:indexPath.row];
   cell.detailTextLabel.text=@"Price";

   return cell;
 }

Example of [UITableViewCell initWithStyle].
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    return cell;
}

UITableViewCell initWithStyle example.
This worked perfect for me (Xcode 4.5.2 and iOS 6.0):

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

if( cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

UILabel *title = (UILabel*) [cell viewWithTag:1000];
UILabel *summary = (UILabel*) [cell viewWithTag:1001];
[title setText:[ tableMainTitle objectAtIndex:indexPath.row]];
[summary setText:[ tableSubTitle objectAtIndex:indexPath.row]];
return cell;
}

End of UITableViewCell initWithStyle example article.