Saturday, June 8, 2013

UITableViewCell initWithStyle reuseIdentifier example in Objective C (iOS).

UITableViewCell initWithStyle reuseIdentifier

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 reuseIdentifier]
A constant indicating a cell style. See “Cell Styles” for descriptions of these constants.
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 reuseIdentifier]
An initialized UITableViewCell object or nil if the object could not be created.

Discussion of [UITableViewCell initWithStyle reuseIdentifier]
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 reuseIdentifier]

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 reuseIdentifier example.
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
          cell = [[[UITableViewCell alloc]

   cell.textLabel.text=[Array objectAtIndex:indexPath.row];

   return cell;

Example of [UITableViewCell initWithStyle reuseIdentifier].
- (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 reuseIdentifier 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 reuseIdentifier example article.