Question

I'm trying to make a UITableView like the native calendar app:

enter image description here

but I'm trying to learn the best way to do this. I'm able to get this for the most part with a switch statement in the cellForRowAtIndexPath method, but I'm having troubles changing the textColor when a cell is selected.

For some reason cell.isSelected is always NO, and I have no way to reload the tableview after another cell is selected anyway.

Should I subclass UITableViewCell for something this simple and store an array of cells?

Any help would be appreciated, thanks.

Was it helpful?

Solution

There is no need to subclass, as stated in the app doc the delegation function:

tableView:willSelectRowAtIndexPath:

should do the trick

EDIT:

Below you find some code that should demonstrate the idea of using the delegate. Please note that this code is untested, as i am currently not in front of my xcode.

-(void)willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (prevIndexPath != nil) {
        UITableViewCell* prevCell = [self.tableView cellForRowAtIndexPath: prevIndexPath];
        prevCell.textLabel.textColor = [UIColor black]; // your initial color here
    }

    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath: indexPath];
    cell.textLabel.textColor = [UIColor green];

    prevIndexPath = indexPath;
}

Make sure to set the UITableViewDelegate protocol to your controlling class that manages your TableView and set the tableViews.delegate to that.

To make this code run, you also have to define a property or variable with the name prevIndexPath. This ones holds the previously selected cell that is needed to revert to the cell to its initial color.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top