0

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];
halfer
  • 18,701
  • 13
  • 79
  • 158
Josh Kahane
  • 15,834
  • 40
  • 127
  • 241

2 Answers2

1

You should use if else statements. For example:

if([indexPath row] == 0){
  [cell setAccessoryView:textField];
}else if([indexPath row] == 1){
  [cell setAccessoryView:textField];
}
Moshe
  • 55,729
  • 73
  • 263
  • 420
0

The process would be the same as setting the cell's textLabel.text property:

if (indexPath.row == 0)
{
    [cell addSubview:textField];
}

Other examples:

Adds a UITextView to all even rows:

if (indexPath.row % 2 == 0)
{
    [cell addSubview:textField];
}

See this SO post for more code: Having a UITextField in a UITableViewCell

Community
  • 1
  • 1
Evan Mulawski
  • 51,888
  • 11
  • 110
  • 142