Friday, June 7, 2013

UITableView dequeueReusableCellWithIdentifier example in Objective C (iOS).


UITableView dequeueReusableCellWithIdentifier

Returns a reusable table-view cell object located by its identifier.

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

Parameters of [UITableView dequeueReusableCellWithIdentifier]
identifier
A string identifying the cell object to be reused. This parameter must not be nil.

Return Value of [UITableView dequeueReusableCellWithIdentifier]
A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.

Discussion of [UITableView dequeueReusableCellWithIdentifier]
For performance reasons, a table view'€™s data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil.[UITableView dequeueReusableCellWithIdentifier]

If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

UITableView dequeueReusableCellWithIdentifier example.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

// Do something to cell

return cell;

Example of [UITableView dequeueReusableCellWithIdentifier].
Basically, UITableView uses a single UITableViewCell instance to draw every cell in the table view. So, when you first create this cell, you should configure it to a state that is common to all cells that will use this instance, independent of whatever row or section is passed in indexPath.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Run");
    CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSArray *keys = [[appDelegate rowersDataStore] allKeys];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        // Configure the cell...

        UILabel *label;
        label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 15, cell.bounds.size.width - 10, 30)] autorelease];
        label.font = [UIFont boldSystemFontOfSize:16];
        label.opaque = NO;
        label.backgroundColor = [UIColor clearColor];
        label.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5];
        label.shadowOffset = CGSizeMake(0,1);
        label.textColor = [UIColor colorWithRed:0x4c/255.0 green:0x4e/255.0 blue:0x48/255.0 alpha:1.0];
        label.tag = 100;
        [cell addSubview:label];
        [label release];

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
        UIImage* img = [UIImage imageNamed:@"odd_slice.png"];
        imgView.image = img;
        cell.backgroundView = imgView;
        [imgView release];

        //Selected State
        UIImage *selectionBackground = [UIImage imageNamed:@"row_selected.png"];
        UIImageView *selectionView = [[UIImageView alloc] initWithFrame:cell.frame];
        selectionView.image = selectionBackground;
        cell.selectedBackgroundView = selectionView;
        [selectionView release];
    }

    UILabel *lbl = (UILabel *)[cell viewWithTag:100];
    switch (indexPath.section) {
        case 0:
            cell.accessoryView = nil;

            lbl.frame = CGRectMake(0, 15, cell.bounds.size.width - 10, 30);
            lbl.textAlignment = UITextAlignmentCenter;
            [label setText:@"Click to add new rower"];
            break;
        case 1:
            UIImage *accessoryImage = [UIImage imageNamed:@"content_arrow.png"];
            UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessoryImage];
            cell.accessoryView = accessoryView;
            [accessoryView release];

            lbl.frame = CGRectMake(20, 15, cell.bounds.size.width - 10, 30);
            lbl.textAlignment = UITextAlignmentLeft;
            [lbl setText:[[[appDelegate rowersDataStore] objectForKey:[keys objectAtIndex:indexPath.row]] objectForKey:@"Name"]];
            break;
    }

    return cell;
}

UITableView dequeueReusableCellWithIdentifier example.
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
    NSString* ident = @"";
    if(indexPath.section == 0) ident= @"complicated";
    if(indexPath.section == 1) ident= @"style1";
    if(indexPath.section == 2) ident = @"style2";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ident];

    if(cell == nil){

       if(ident == @"complicated"){
          cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];
         // do excessive subview building
       }
       if(ident == @"style1"){
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyle1 reuseIdentifier:ident] autorelease];
       }

       if(ident == @"style2"){
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyle2 reuseIdentifier:ident] autorelease];
       }

    }
    if(ident == @"complicated"){
       // change the text/etc (unique values) of our many subviews
    }
    if(ident == @"style1"){
      [[cell textLabel] setText:@"Whatever"];
    }
    if(ident == @"style2"){
      [[cell textLabel] setText:@"Whateverelse"];
    }

    return cell;
}

End of UITableView dequeueReusableCellWithIdentifier example article.