Take a look at this question and the second answer gives a great description.
In short you should not be using the viewDiLoad
callback but instead the
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
From here you can customise each cell's background as you wish, just reload the row when the user clicks.
How to customize the background color of a UITableViewCell?
EDIT
Now because you added your code I can clearly see the problem:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BTSTicketsCellIdentifier";
CRHomeCategCell *cell = (CRHomeCategCell *)[_tblCateg dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundView = nil;
}
This does not do what you think it does. dequeueReusableCellWithIdentifier:CellIdentifier
gives you a NEW instance of a cell based off the one identified by the identifier.
You are not getting a reference to the row here, you are creating a new row and settings its background to nil.
Your code should be something more like this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.row==0){
if(cell.backgroundView == nil)
{
cell.backgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"active-tab.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
NSLog(@"willDisplayCell");
}
else
{
cell.backgroundView = nil;
NSLog(@"willHideCell");
}
}
}
This is not a great solution, I personally would do something more like having this custom cell hold a boolean and switch its state and check that. But this is up to you to develop, this is the general idea of how it should work.
EDIT 2:
Since you are determined to run it inside didSelectRowAtIndexPath
and also completely incapable of doing any level of research or putting any effort into your work might I suggest the method:
tableView cellForRowAtIndexPath:<#(NSIndexPath *)#>