Iphone 仅在某些单元格中添加子视图

Iphone 仅在某些单元格中添加子视图,iphone,objective-c,ipad,uitableview,cell,Iphone,Objective C,Ipad,Uitableview,Cell,我在UITableView中有10个单元格/行,我将其中四个单元格设置为具有如下文本: if (indexPath.row == 0) { cell.textLabel.text = @"Before School"; } 我在里面做这一切: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 我正在尝试仅向特定

我在UITableView中有10个单元格/行,我将其中四个单元格设置为具有如下文本:

if (indexPath.row == 0) {
        cell.textLabel.text = @"Before School";
    }
我在里面做这一切:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
我正在尝试仅向特定行添加UITextField。我怎样才能做到这一点?我已使用以下方法成功地将UITextField添加到所有或无UITextField中:

[cell addSubview:textField];

您应该使用
if-else
语句。例如:

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

该过程与设置单元格的
textlab.text
属性相同:

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

将UITextView添加到所有偶数行:

if (indexPath.row % 2 == 0)
{
    [cell addSubview:textField];
}
有关更多代码,请参阅本SO帖子: