开发者

iPhone UITableView populated by checked Table Cells. How?

I came upon this blog post the other day and literally have no idea how to implement it, logically.

Any ideas?

I was thinking it involves core data, with 'power' being another entity in a to-many relationship, besides that开发者_运维知识库, I'm lost.


You can support the checking and unchecking of "power" cells using an NSMutableSet to keep track of the selected powers. This works whether you use Core Data or some other method to supply the powers data to the UITableView.

For example, in the table's data source implementation:

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

    // determine the power from the IndexPath.row

    if ([selectedPowerSet member:power])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

and in the table's delegate (usually the same class) implementation:

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

    if ([selectedPowerSet member:power]) {
        [selectedPowerSet removeObject:power];
    } else {
        [selectedPowerSet addObject:power];
    }

    [tableView reloadData];
}


If you want to implement the view hierarchy, you just start with a navigation project. The Superhero tableview controller is your root controller and the powers tableview is a sub controller.

In your core data model, you would have two entities, a superhero entity and a power entity. Each superhero entity would have a name and then a to-many relationship to power entities. The power entity would have a name and (optionally) a reciprocal to many relationship to all the superhero entities that have the power. (Not really needed but in this case but good practice.)

The superhero tableview datasource would do a fetch for all superhero entities and then populate the text of each cell with the name of each superhero. When the user clicked on a hero, the superhero table view would tell the navcontroller would push the power tableview on the stack and hand the selected superhero entity to the power tableview datasource. The power tableview datasource would then populate the table with with the powers in the relationship with the superhero entity. It would also create power entities as needed an add them to the relationship.

You don't need to use core data for this if the amount of data you are using is small. You could just create a dictionary where each key was a superhero's name and each value was an array of power names. The superhero tableview datasource would populate with the keys and the powers tableview datasource would populate with the individual elements in the value array.

I suggest you start with UINavigationController.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜