0

I have UITableView in which i want to add Background image on first cell in viewDidLoad method, after adding image on first cell, when user selects any other row i want to hide my background image.

Is it possible to do ?

Please help and thanks in advance.

Edit:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
    if(indexPath.row==0){
        cell.backgroundView = [ [UIImageView alloc] initWithImage:[[UIImage imageNamed:@"active-tab.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
                flag=true;
         NSLog(@"willDisplayCell");
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (flag==true) {
       cell.backgroundView = nil; //How to get `cell` here ?
       //How to remove BGImage from first cell ???
    }
}
HRM
  • 2,097
  • 6
  • 23
  • 37
Krunal
  • 6,440
  • 21
  • 91
  • 155
  • 1
    You should include more of your code to make it easier for people to see how your trying to do it rather than guessing – Simon McLoughlin Jan 07 '14 at 09:30
  • don't forget to mark top answer and upvote the answer(s) that helped you. People facing the same issue will want to know what fixed your issue and those who have put time into answering your question deserve the reputation points – Simon McLoughlin Jan 07 '14 at 11:59
  • I removed my downvote purely because the code was added which helped identify the issue. I do however feel the question was still a little lacking and next time I would encourage you to put in a little more effort as others will be more likely to close a question when seeing that – Simon McLoughlin Jan 07 '14 at 12:18

2 Answers2

1

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 *)#>
Community
  • 1
  • 1
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
  • `willDisplayCell` will add image on first cell but how to remove that image, when user selects other row. – Krunal Jan 07 '14 at 09:31
  • as I mentioned in my answer when the user selects the row, reload the cell and the above callback will be triggered again, from there you can check if its the right cell and then modify it again – Simon McLoughlin Jan 07 '14 at 09:33
  • [tableview reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>] will reload specific rows, [tableview reloadData] will reload all of them. Sounds like you need to read the documentation a little better – Simon McLoughlin Jan 07 '14 at 09:51
  • Check **EDIT** in my question – Krunal Jan 07 '14 at 10:13
  • My log shows `willHideCell` on startup but it should call `willDisplayCell` bocz i want to add image on startup – Krunal Jan 07 '14 at 10:31
  • @Krunal ok so clearly the if statement if(cell.backgroundView == nil) in the above code is false. Did you read the text underneath the code where I explained what you should do and that this was not a great solution ??? – Simon McLoughlin Jan 07 '14 at 10:33
  • Check new **EDIT** in my question – Krunal Jan 07 '14 at 10:44
  • @Krunal see yet another edit to my answer. Please do some level of work to solve your issues. You will get questions closed if you refuse to do any searching online or through docs yourself – Simon McLoughlin Jan 07 '14 at 10:48
0

it works add table view delegates in your class

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
     *)indexPath  
    {  

     if((indexPath.row)==0)  {
      cell.backgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 
    }

    }


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

yourcustomcell *cell = [tableView cellForRowAtIndexPath:indexPath];
          cell.backgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"pressed.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 

or

      cell.backgroundView = nil; 

[tableview reloadData];

    }
codercat
  • 22,873
  • 9
  • 61
  • 85
  • How to get `cell` in `didSelectRowAtIndexPath` ? – Krunal Jan 07 '14 at 09:36
  • UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; @Krunal – codercat Jan 07 '14 at 09:40
  • I wrote this but doesn't help, I have custom UITablviewCell, `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.backgroundView = nil; }` – Krunal Jan 07 '14 at 09:43
  • change to youcustomcell instead of UItableviewcell @Krunal – codercat Jan 07 '14 at 09:46
  • Tried this but still problem `static NSString *CellIdentifier = @"BTSTicketsCellIdentifier"; CRHomeCategCell *cell = (CRHomeCategCell *)[_tblCateg dequeueReusableCellWithIdentifier:CellIdentifier]; cell.backgroundView = nil;` – Krunal Jan 07 '14 at 09:48
  • finally reload the tableview – codercat Jan 07 '14 at 09:48
  • There is no warning or error but i am unable to remove image from first cell – Krunal Jan 07 '14 at 09:55
  • @Krunal as I mentioned in my comment on your question, please edit your question and add the code you have at the minute, it is very difficult for us to know what the issue is when we can't see your code, and snippets in comments are not helpful – Simon McLoughlin Jan 07 '14 at 10:06
  • Check **EDIT** in my question – Krunal Jan 07 '14 at 10:12