How to get user defined background color in three20's TTTableViewController
having a TTTableViewController instance, I try to set the background of the currently selected cell. I am aware of the style sheets concept in Three20, but it just offers:
- (UITableViewCellSelectionStyle)tableSelectionStyle {
return UITableViewCellSelectionStyleGray;
}
with standard options UITableViewCellSelectionStyleNone, UITableViewCellSelectionStyleBlue, UITableViewCellSelectionStyleGray.
Second, I am aware, that UITableView implementations would just take a subclassed UITableViewCell. One would set selectedBackgroundView of that cell class:
UIView *myBackVi开发者_Go百科ew = [[UIView alloc] initWithFrame:self.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
self.selectedBackgroundView = myBackView;
[myBackView release];
How can I set the background color of a selected cell in a three20's TTTableViewController instance?
I figured this out tonight. I went into three20UI.xcodeproj and had a look for where the UITableViewCell was being constructed, that is the Data Source, and then I subclassed that data source which I am using to set up my table.
For example, if you've table that's using a TTSectionedDataSource, then you subclass that, let's say we call it MySectionedDataSource, and then inside the implementation for that subclass, you'll want to do something like this:
- (void)tableView:(UITableView*)tableView
cell:(UITableViewCell*)cell
willAppearAtIndexPath:(NSIndexPath*)indexPath {
UIView *view = [[UIView alloc] initWithFrame: CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.width, cell.height)];
view.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
cell.selectedBackgroundView = view;
}
That should do it.
At first I tried to set the selectedBackgroundView inside of
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
but that doesn't seem to work. I searched to code to find out why, i.e. if something in a controller, delegate, cell, or data source were re-setting the bg selection, but nothing there. I ended up having to search so technically I didn't solve it by myself... :)
Good luck.
精彩评论