Adding a subview larger than cellHeight to a UITableViewCell?

后端 未结 4 1670
情歌与酒
情歌与酒 2020-12-30 04:45

I\'m trying to add a subview to a UITableViewCell and the design that I\'m working from demands that this particular subview (an image) needs to be larger than the actual UI

4条回答
  •  旧时难觅i
    2020-12-30 05:13

    UPDATE:

    So I've done some more experimentation and the following solution still works without having to set the background of the cell to transparent, this involved moving the z order of the covered cell. This works with highlighting and selecting of the other cell (via the relevant callbacks), and if the two cell's backgrounds are different colors. Solution is as follows (you can ignore the didHighlight and didSelect methods if they don't matter to you):

    (note that "covered row" is the one whose content we are trying to keep visible and In my case its content goes slightly into the row above, which was clipping it)

    -(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0 && indexPath.row == ROW_ABOVE_COVERED_ROW)
        {
            NSIndexPath * rowbelow = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
            UITableViewCell* cell = [tableView cellForRowAtIndexPath:rowbelow];
            [cell.superview bringSubviewToFront:cell];
        }
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0 && indexPath.row == ROW_ABOVE_COVERED_ROW)
        {
            NSIndexPath * rowbelow = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
            UITableViewCell* cell = [tableView cellForRowAtIndexPath:rowbelow];
            [cell.superview bringSubviewToFront:cell];
        }
    }
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section == 0 && indexPath.row == COVERED_ROW)
        {
            [cell.superview bringSubviewToFront:cell];
            cell.contentView.superview.clipsToBounds = NO;
        }
    }
    

    NOTE: you should also set the background color of your content to clear, or it will adopt the bgcolor of the rest of your cell, and so when you manage to bring your content to the front of the covering cell, it will take the background color with it and leave a nasty looking block in the other cell (in my case my only content was the detailTextLabel and the textLabel):

    // in cellForRowAtIndexPath:
    [cell setBackgroundColor:[UIColor redColor]]; //using red for debug
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    

    I hope that's helpful to anyone else trying this....

    ORIGINAL:

    For me the solution was to use:

    self.contentView.superview.clipsToBounds = NO;
    

    My cells were already transparent, but my content was still getting clipped. In my case I was using a custom cell which moves it's content up in layoutSubviews. So layoutSubviews for my custom cell wound up as follows:

    -(void)layoutSubviews
    {
        [super layoutSubviews];
        self.contentView.frame = CGRectOffset(self.contentView.frame, 0, -11);
        self.contentView.superview.clipsToBounds = NO;
    }
    

    I don't know if this would work if the cell above was opaque, or if the cells were to highlight when pressed, whether this would cover up my content.

    However, I didn't need to make the cell transparent again in the viewWillDisplayCell callback method - doing it in the normal cellForRowAtIndexPath was sufficient

提交回复
热议问题