Diffy
Diffy

Reputation: 735

Transparent background for commitEditingStyle

When the delete button appears after swiping the third cell, the background is clipped. How can I fix this? here's the code when making the custom cell.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    
      (NSIndexPath *)indexPath
     {
         static NSString *CellIdentifier = @"customCell";

         BookMarksCustomCell *cell = (BookMarksCustomCell *)[tableView  
         dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            NSArray * topLevelObjects = [[NSBundle mainBundle]  
            loadNibNamed:@"BookMarksCustomCell" owner:self options:nil];

        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell = (BookMarksCustomCell *)currentObject;
                break;
            }
         }        
    }

    .....more logic stuff.

    //alternating cell background.
    if([indexPath row] % 2 == 0)
        cell.contentView.backgroundColor = [UIColor colorWithRed:234.0/255.0 
        green:234.0/255.0 blue:234.0/255.0 alpha:1.0];

    ......
   }

http://i228.photobucket.com/albums/ee262/romano2717/photo3.png

Upvotes: 1

Views: 464

Answers (1)

lyricsboy
lyricsboy

Reputation: 3009

Your code is modifying the background color of the contentView property of the cell. This view gets resized when the delete button appears, and so you need to instead set the background color of the cell itself (which is also a UIView subclass). This should fix it.

Also, to account for cell reuse and changing indices, you should set the background color no matter whether the current cell is even or odd. Always explicitly set the color to something (even if it is white) so that when you scroll you don't get odd effects with the reused cells.

I also just noticed in the UITableViewCell docs this note:

If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source.

So, do it there instead.

Upvotes: 3

Related Questions