this tutorial show how to insert custom cell as uitableviewcell when any row on uitableview is selected.
first created view controller with uitableview ,then create custom cell as you like ,you can subclass uitableviewcell and created one.
the trick here is when row is selected we reload table and return new cell view for selected row.thats all
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedCellIndexPath != nil
&& [selectedCellIndexPath isEqual:indexPath] ) {
static NSString *CustomCellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.label.text=[self.tableData objectAtIndex:indexPath.row];
cell.textField.text=[self.tableData objectAtIndex:indexPath.row];
return cell;
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[self.tableData objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
self.selectedCellIndexPath = indexPath;
if(selectedCellIndexPath != nil
&& [selectedCellIndexPath isEqual:indexPath]) {
[self.tableView reloadData];
}
}
you can download the code from below.
InsertCustomCellOnTableRow
Related posts: