Baub
Baub

Reputation: 5044

Update UITableViewCell Subview

I have a very simple question that I can't (for the life of me) figure out. I know I'm going to smack myself when I hear the answer.

I have a UITableView, and on it, UITableViewCell subclasses. Each cell has a UIButton subview.

I need the button on each cell to be disabled if a boolean value is false; the button should be enabled if/when the boolean value changes to true. I will observe KVC. Where should I have the handler for this? In the UIViewController? If so, what is the best way to tell the cells that they need to disable/enable the button?

Upvotes: 3

Views: 885

Answers (2)

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

I see two approaches.

1) reloadData (as was suggested earlier)

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"data.boolean"]) {
        [self.tableView reloadData];
    }
}

2) The second is to iterate the visible cells.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"data.boolean"]) {
        for (MyTableViewCell *cell in [self.tableView visibleCells]) {
            cell.button.hidden = !self.data.boolean;
        }
    }
}

Upvotes: 0

sapi
sapi

Reputation: 10224

I would implement the show/hide functionality in cellForRowAtIndexPath, and call [tableView reloadData] (or a more specific reload/refresh call) when a change to the boolean value was made.

Upvotes: 4

Related Questions