Change background for UITableViewCell

Discussion in 'iPhone/iPad Programming' started by Sergio10, Dec 4, 2009.

  1. macrumors regular

    Joined:
    Oct 3, 2007
    #1
    Hi,

    I need to change background for the item(UITableViewCell).
    And problem is next: when change background with image once it works Ok, but when I change for the second time(after refreshing table) it doesn't change.
    It works only when use solid colors for changing(blueColor, greenColor) background.

    So here is a code:
    PHP:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {    
     static 
    NSString *CellIdentifier = @"Cell";

     
    MyCell *cell = [[[MyCell allocinitWithFrame:CGRectZero  reuseIdentifier:CellIdentifierautorelease];

     if(...)
    {
     [
    cell setMyBackground:@"someBakground.png"];
    }
    else
    {
     [
    cell setMyBackground:@"someBakground2.png"];
    }

    return 
    cell;
    }

    . . .

    - (
    void)setMyBackground:(NSString*)anImage
    {
    [
    self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:anImage]]];
    }
    How to solve the problem?

    Thank you.
     
  2. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #2
    What version of iPhone OS are you building for? Different versions of the OS seemed to handle the cell's background color in different ways. Are you building for 2.X because you call "initWithFrame:reuseIdentifier:"?

    Does your "setMyBackground:" method belong to your "MyCell" class?

    You are calling "setMyBackground:" in "tableView:cellForRowAtIndexPath:". This might be a better method to call in your "tableView:willDisplayCell:forRowAtIndexPath:" method.

    You are also creating a new cell each time you call "tableView:cellForRowAtIndexPath:" You want to check to see if your table view has a reusable cell available before you alloc and init an brand new instance.
     
  3. thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #3
    I use iPhone SDK 3.0. How to create cell for 3.0 iPhone OS?
    Yes. Here is my implementation of this method
    PHP:
    - (void)setMyBackground:(NSString*)aBackground
    {
      [
    self setBackgrounColor:[UIColor colorWithPatternImage:[UIImage imageNamed:aBackground]]];
    }
    Sorry, but I can't change logic in such way.

    I think reuse cell because use "initWithFrame: reuseIdentifier". Could you please describe how to fix it if I'm wrong in more details?

    Thank you.
     
  4. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #4
    When you alloc and init a new table view cell, you give it a reuse identifier, but you get the benefit by checking to see if the table view can give you a cell *before* you go to the trouble of making a new cell.

    You pick a reuse identifier, then you send that identifier to the table view to ask for a cell. If that cell does not exist (it returns nil), then you go to all the trouble of making a new cell.

    I have to ask why you can not use that method? Apple recommends that changes to the graphics state of a cell (such as background color) should be made in "tableView:willDisplayCell:forRowAtIndexPath:". In fact, I can remember times when I was trying to set background colors and I was having problems when I changed the colors in "tableView:cellForRowAtIndexPath:"
     
  5. thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #5
    It helped. Thank you.

    Could you please show me an example in my case?
     

Share This Page