UIColor colorWithPatternImage uses only one image

22,900

Solution 1

As far I can see this is not or no longer true. I have here a couple of UITableViewCells, where each has a different backgroundImage, without any problems.

Solution 2

What is Cell1? Where (in what method) are you setting these?

I would say that you should be doing all this in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {      
      // create cell
    }

    // Set up the cell...

    // set up a background color
    if (something)
        cell.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"red.png"]];
    else (another)
        cell.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"green.png"]];
    else
        cell.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"blue.png"]];
}
Share:
22,900
user14322501
Author by

user14322501

Updated on October 23, 2020

Comments

  • user14322501
    user14322501 over 3 years

    I'm trying to give tableviewcell's different backgroundColors with colorwithPatternImage and it is not working as expected. The documentation says nothing about only being able to use one pattern at a time.

    Say I have 3 rows and I set the background like so:

    Cell1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"red.png"]];
    
    Cell2.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"green.png"]];
    
    Cell3.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"blue.png"]];
    

    All 3 rows would be red. It's as if there is some global color that is being returned.

    colorWithPatternImage returns kCGColorSpaceModelPattern 1 for each call no matter what image is passed in. If it was true that you only have 1 global pattern at a time, then the color should be the last one set, in other words blue.

    This makes no sense. Does anyone have any inside expertise on what Apple is doing here?

    EDIT I even use a different pattern in a completely separate view and it still affects the other view's patterns. I am convinced, though the documentation doesn't state this, that you are limited to one UIColor image pattern at a time. Sad.

  • user14322501
    user14322501 over 14 years
    I am doing it in that method. Thus, a cell is returned so the table can display it. Cell[1-3] are actually each a case statement in a switch,