如何使自定义绘制的UITableViewCell正确调整大小?

时间:2023-01-12 12:05:49

For performance reasons, I draw the strings for my UITableViewCell in a custom view that overrides its drawRect method to draw strings directly in the view rectangle using NSString:drawInRect. This is similar to Apple's TableViewSuite Example 5-CustomTableViewCell.

出于性能原因,我在自定义视图中绘制UITableViewCell的字符串,该视图覆盖其drawRect方法,使用NSString:drawInRect直接在视图矩形中绘制字符串。这类似于Apple的TableViewSuite示例5-CustomTableViewCell。

However, when I invoke setEditing on the cell to bring up the delete button, the view ends up with a squeezed appearance after the animation completes. To demonstrate this, invoke setEditing:YES on the CustomTableViewCell example mentioned above and observe the distortion. Is there any way around this or should I just revert back to using UILabels for my text?

但是,当我在单元格上调用setEditing以调出删除按钮时,视图会在动画完成后以挤压外观结束。为了演示这一点,请在上面提到的CustomTableViewCell示例上调用setEditing:YES并观察失真。有什么方法可以解决这个问题,还是应该恢复使用UILabels作为我的文本?

4 个解决方案

#1


5  

I had a similar problem with a UIView inside a UITableViewCell. I solved it by changing the UIView's contentMode to UIViewContentModeLeft. (I wrote it up here, with screenshots.)

我在UITableViewCell中遇到了类似的UIView问题。我通过将UIView的contentMode更改为UIViewContentModeLeft来解决它。 (我在这里用截图写了。)

#2


2  

I had this problem too, and in my case I fixed it by handling the 2 states in my drawRect method, one while editting, the other while not. In other words I accounted for the size of the delete button, and got my UI to repaint the cell differently. I'm not sure if it's the most efficient way to go, but here is the code that I used to force a repaint:

我也有这个问题,在我的情况下,我通过处理drawRect方法中的2个状态来修复它,一个是编辑,另一个是不编辑。换句话说,我考虑了删除按钮的大小,并让我的UI以不同方式重新绘制单元格。我不确定它是否是最有效的方法,但这里是我用来强制重绘的代码:

-(void)_refreshTableAndCells{
    //refresh the table
    [myCustomTableView reloadData];
    //refresh all the visible cells
    for (UITableViewCell *cell in myCustomTableView.visibleCells){
        LocationCellView *locationCell = [cell.contentView.subviews objectAtIndex:0];
        [locationCell setNeedsDisplay];
    }

}

I'm an Objective-C n00b though, so I'd be more than happy for someone to suggest a better way than this.

我是Objective-C n00b,所以我很高兴有人建议比这更好的方法。

#3


1  

I usually just modify the x and width values (or whatever else) of whatever I want to be different when editing or not. UITableView automatically calls layoutSubviews when you begin editing, so you don't have to loop through your cells and do it yourself.

我通常只是在编辑或不编辑时修改我想要的不同的x和宽度值(或其他任何东西)。开始编辑时,UITableView会自动调用layoutSubviews,因此您无需遍历单元格并自行完成。

- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat editingPadding = 5.0;
    self.textLabel = CGRectMake((self.editing ? self.textLabel.frame.origin.x + editingPadding : self.textLabel.frame.origin.x), self.textLabel.origin.y, (self.editing ? self.textLabel.frame.size.width - editingPadding : self.textLabel.frame.size.width), self.textLabel.frame.size.height);
}

#4


0  

Try setting the contentMode of your own custom view (which resides inside the cell's contentView) to UIViewContentModeLeft. The "squeezing" is due to the fact that the default contentMode is UIViewContentModeScaleToFill.

尝试将您自己的自定义视图(位于单元格的contentView内)的contentMode设置为UIViewContentModeLeft。 “挤压”是由于默认的contentMode是UIViewContentModeScaleToFill。

#1


5  

I had a similar problem with a UIView inside a UITableViewCell. I solved it by changing the UIView's contentMode to UIViewContentModeLeft. (I wrote it up here, with screenshots.)

我在UITableViewCell中遇到了类似的UIView问题。我通过将UIView的contentMode更改为UIViewContentModeLeft来解决它。 (我在这里用截图写了。)

#2


2  

I had this problem too, and in my case I fixed it by handling the 2 states in my drawRect method, one while editting, the other while not. In other words I accounted for the size of the delete button, and got my UI to repaint the cell differently. I'm not sure if it's the most efficient way to go, but here is the code that I used to force a repaint:

我也有这个问题,在我的情况下,我通过处理drawRect方法中的2个状态来修复它,一个是编辑,另一个是不编辑。换句话说,我考虑了删除按钮的大小,并让我的UI以不同方式重新绘制单元格。我不确定它是否是最有效的方法,但这里是我用来强制重绘的代码:

-(void)_refreshTableAndCells{
    //refresh the table
    [myCustomTableView reloadData];
    //refresh all the visible cells
    for (UITableViewCell *cell in myCustomTableView.visibleCells){
        LocationCellView *locationCell = [cell.contentView.subviews objectAtIndex:0];
        [locationCell setNeedsDisplay];
    }

}

I'm an Objective-C n00b though, so I'd be more than happy for someone to suggest a better way than this.

我是Objective-C n00b,所以我很高兴有人建议比这更好的方法。

#3


1  

I usually just modify the x and width values (or whatever else) of whatever I want to be different when editing or not. UITableView automatically calls layoutSubviews when you begin editing, so you don't have to loop through your cells and do it yourself.

我通常只是在编辑或不编辑时修改我想要的不同的x和宽度值(或其他任何东西)。开始编辑时,UITableView会自动调用layoutSubviews,因此您无需遍历单元格并自行完成。

- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat editingPadding = 5.0;
    self.textLabel = CGRectMake((self.editing ? self.textLabel.frame.origin.x + editingPadding : self.textLabel.frame.origin.x), self.textLabel.origin.y, (self.editing ? self.textLabel.frame.size.width - editingPadding : self.textLabel.frame.size.width), self.textLabel.frame.size.height);
}

#4


0  

Try setting the contentMode of your own custom view (which resides inside the cell's contentView) to UIViewContentModeLeft. The "squeezing" is due to the fact that the default contentMode is UIViewContentModeScaleToFill.

尝试将您自己的自定义视图(位于单元格的contentView内)的contentMode设置为UIViewContentModeLeft。 “挤压”是由于默认的contentMode是UIViewContentModeScaleToFill。