Monday 19 September 2011

Multiple Reuse Identifier In UITableViewCell


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    if (indexPath.row == [temparray count]) {
           CellIdentifier = @"textCell";
    }
    else {
           CellIdentifier = @"otherCells";
    }


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
    }
    UITextField *txtField = [[UITextField alloc] initWithFrameCGRectMake(5, 5, 150, 30)];
    txtField.tag = 45;
    int lastRow = [temparray count];
    if (indexPath.row == lastRow) {
            NSLog(@"%d",indexPath.row);
            [cell.contentView addSubview:txtField];
    }
    else {
            UIView *theView = [cell viewWithTag:45];
           [theView removeFromSuperview];
           cell.textLabel.text = [temparray objectAtIndex:[indexPath row]];
    }
  return cell;
}


Using above function one can create the table view which will contain the tempArray data and in last row a UITextField.