Vishal
Vishal

Reputation: 2060

Change sub view of all cells in UITableView

I'm creating an app which contains a screen that shows a table view with custom cells. Each cell contains two labels and a subview, which further contains other subviews. I'm handling the click event on the cell to hide/show the subviews within the subview in the cell. How can I make it so that when I click on a single cell, the subview of all the cells will change?

It is like the Stock application in iPhone (using iOS 7), here is a screenshot:

enter image description here

As in the image above, when you click on any of the green box, all the boxes change to reflect the same type of value.

Please let me know if this approach is fine, or how this can be implemented.

Upvotes: 0

Views: 584

Answers (2)

Greg
Greg

Reputation: 25459

To do something as in Stock app you should handle two method cellForRowAtIndexPath: and click action method.

In cellForRowAtIndexPath: you should do the check which cell/button was pressed and display value base on it:

//Pseudo code
//cellForRowAtIndexPath
if (cellNo3Pressed)
{
    //set up text with the right value.
}
else if (otherCell)
{
    //set up text with the right value.
}

This will handle the cell which are not visible on the screen.

The next action method should handle nice animation on all of the visible cell:

NSArray *paths = [tableView indexPathsForVisibleRows];

for (NSIndexPath *path in paths)
{
    UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:path];
    //Animate changes for cell
}

Upvotes: 1

Mick MacCallum
Mick MacCallum

Reputation: 130212

There are a couple ways of doing this. The first that comes to mind would be to handle the different states within the UITableViewCell subclass, and just reload the visible cells:

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];

If you're looking for more control over the process though, this process could also be achieved by changing the state future cells should load into, and then calling a method on every visible cell. This would provide you with an easy way to have complete control over how the contents of the cell update.

// Change flag for cell state then...
for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows]) {
    if (condition) {
        MyCellSubclass *cell = (MyCellSubclass *)[self.tableView cellForRowAtIndexPath:indexPath];
        [cell someMethodWithArg:(id)state];
    }
}

Upvotes: 1

Related Questions