0

I have a tableView, which initially has 4 sections with 1 row in each. Depending on the user interaction with the first row, I need to insert or delete a second row in the section. After that, I need to do the setup of the cell, which is next after tapped. Here's my code for handling interaction:

-(void) tableViewSwitchToggled: (UISwitch*) sender{
    targetSection = sender.tag; //each switch is added to cell's contentview and it's tag is set to indexPath.section
    UITableView* tableView = ...

    if (someBool){
        [tableView insertRowsAtIndexPaths: [NSArray arrayWithObject: [NSIndexPath indexPathForRow: 1 inSection: targetSection]] withRowAnimation: UITableViewRowAnimationTop];
        UITableViewCell* detailsCell = [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: targetSection + 1]]; //I try to get the first row of the next section and it is always nil!!!
        //do something with detailsCell content
    }
    else{
        [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: [NSIndexPath indexPathForRow: 1 inSection: targetSection]] withRowAnimation: UITableViewRowAnimationRight];
        UITableViewCell* detailsCell = [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: targetSection + 1]]; //and it works perfectly, giving me the cell address
         //do something with detailsCell content
    }
}

So here's my question - why do I get a nil cell after I insert a row? Isn't the tableView supposed to have all cells already allocated? Thanks for your help!

4

1 回答 1

1

您只能获得对可见单元格的引用。您可以向 tableView 询问可见的单元格数组(或其索引路径,忘记哪个)。细胞是一种在其可见时存在的暂时性事物 - 这就是您可以回收它们的原因。

您需要做的是在代码中注意,当该单元格重新出现时,您想对它做一些特别的事情。

于 2012-07-22T15:25:42.497 回答