UITableViewCell show white background and cannot be modified on iOS7

后端 未结 14 1289
轻奢々
轻奢々 2020-11-28 18:42

I\'ve implemented a custom table view cell class that inherit from UITableViewCell. The tableview contains a background image, so I want cell\'s background to b

相关标签:
14条回答
  • 2020-11-28 18:56

    This is definitely an iOS bug. In iOS 8, setting background color in Interface Builder works fine for iPhone, but in iPad it's always whiteColor. To fix it, in the cellForIndexPath data source message, putting this in:

    cell.backgroundColor = cell.backgroundColor

    And it will work. This is exactly why I said it's a bug since the code itself is pretty much bullsh*t, but it works.

    0 讨论(0)
  • 2020-11-28 19:02

    I found that none of the above worked from XCode 5.1.1, iOS 7.1.

    If you are using Interface Builder with prototype cells, select the prototype cell, then from the attributes selector in the View section, change the Background form default to Clear Color:

    enter image description here

    This seems to work fine. None of the above code changes are needed, either--this is a pure IB solution.

    0 讨论(0)
  • 2020-11-28 19:03

    Write this in your cellForRowAtIndexPath method before return cell;

    cell.backgroundColor = [UIColor clearColor];
    
    0 讨论(0)
  • 2020-11-28 19:03

    For me setting cell.backgroundColor = cell.contentView.backgroundColor; either in tableView:willDisplayCell:forRowAtIndexPath: or tableView:cell:forRowAtIndexPath: did the job.

    This is because I set the contentView's background colour in Interface Builder as desired.

    0 讨论(0)
  • 2020-11-28 19:04

    As Apple DOC said (UITableViewCell Class Reference):

    ... In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate.

    So for my case that to show cells with transparent background, just need to implement the delegate method in the table view controller like below:

    - (void)tableView:(UITableView *)tableView
      willDisplayCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath
    {
      [cell setBackgroundColor:[UIColor clearColor]];
    }
    

    Just Note: As @null said, "...there seems to be a bug in interface builder...", I'm not totally sure whether it does have the bug, but seems so cause his comment got several up votes. So there might something wrong if you use IB. :)

    0 讨论(0)
  • 2020-11-28 19:04

    The accepted answer did not fix the problem for me. I had to do this:

    1. In the interface builder, select the table view. Then from the attributes inspector, from the View section, set the Background to transparent (0% opacity).

    enter image description here

    1. From the table's data source class cellForRowAtIndexPath method:

      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
          reuseIdentifier:CellIdentifier];
      cell.backgroundColor = [UIColor clearColor]; //Make cell transparent
      
    0 讨论(0)
提交回复
热议问题