展开单元格并将子视图添加到选定的单元格?

时间:2014-09-18 14:33:52

标签: ios uitableview

我有一个表格视图,可以从Web服务加载数据,例如标签x3,按钮等。单元格类型是自定义的,我以编程方式展开单元格并向其添加子视图。行扩展正常,子视图添加到单元格。问题是按钮等子视图不仅会添加到选定的单元格中,还会添加到其他一些单元格中。如何避免将子视图添加到其他单元格中。

要刷新按钮上的特定单元格,请使用:

  [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowNo inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];

在cellForRowAtIndexPath方法中,我使用:

if (buttonPressed ==  YES)

{

 test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                test.frame = CGRectMake(10, 121, 260, 28);
  [cell.contentView addSubview:test];


}

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要做的是存储按下按钮的每个单元格的indexpath / indexpath.row,并将其添加到NSMutableArray。 在cellForRowAtIndexPath方法中检查按下的按钮:

if([self.buttonarray containsObject:indexpath) {
    //Code to add/remove button accordingly
    //Add button with tag as indexpath.row and for removal get the button with tag and remove button
    also remove the indexpath of button from self.button array
} else {
    //Code to add/remove button accordingly
    //Add button with tag as indexpath.row and for removal get the button with tag and remove button
    also remove the indexpath of button from self.button array      
}