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
  • 19,824
  • 17
  • 99
  • 186
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253

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
  • 57,511
  • 78
  • 272
  • 425
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
  • 54,662
  • 15
  • 117
  • 144