开发者

how to load cell selection into parent uitableview

I have found an example that I am trying to wrap my head around, I have a subview thats loading values from my online resource into the section of uitableview cells.. everything is working sweet however now I want to capture the cell selection and pass that data back to the parent view cell that was selected. I think this example is what i need to get it working but I am struggling to understand what some of the variabl开发者_如何学JAVAes are for in the example and am hoping someone can help me understand it abit better.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // do usual stuff here including getting the cell

    // determine the data from the IndexPath.row

    if (data == self.checkedData)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // determine the selected data from the IndexPath.row

    if (data != self.checkedData) {
       self.checkedData = data;
    }

    [tableView reloadData];
}

my main concerns are with data and self.checkedData is data the data coming back from the array of values that have been parsed? and what is checkedData? basically I want to have those check marks at the end of the cell when selected.


I might be wrong considering there is no much context with that few lines of code but I'll give it a try.

First of all, it looks like you can have only one cell checked at a time.

Data seems to be the current cell and checkedData a way to keep track of which cell will get the checkmark.

Whenever a cell is selected it's marked:

if (data != self.checkedData) {
   self.checkedData = data;
}

And the controller asks the TableView to redraw (i.e. reload) itself again:

[tableView reloadData];

When that action is fired-up, the delegate calls tableView:cellForRowAtIndexPath: (amongst other methods) and if you are the selected cell you get a checkmark:

if (data == self.checkedData)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} 

Otherwise, you don't:

else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜