Search code examples
iphoneobjective-cipaduitableviewcell

Only addSubView in certain cells


I have 10 cells/rows in a UITableView and I have set four of these cells to have some text like so:

if (indexPath.row == 0) {
        cell.textLabel.text = @"Before School";
    }

I'm doing all of this inside:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

I am trying to add a UITextField to only specific rows. How can I achieve this? I have managed to add a UITextField to either all or none of them using:

[cell addSubview:textField];

Solution

  • You should use if else statements. For example:

    if([indexPath row] == 0){
      [cell setAccessoryView:textField];
    }else if([indexPath row] == 1){
      [cell setAccessoryView:textField];
    }