简体   繁体   English

在删除滑动时调整UITableViewCell的高度

[英]Adjust UITableViewCell height on delete swipe

I have a UITableViewCell with dynamic height based on textual content. 我有一个基于文本内容具有动态高度的UITableViewCell In tableview:heightForRowAtIndexPath: the height gets calculated. tableview:heightForRowAtIndexPath:计算高度。 This works well. 这很好。

When the cell enters editing mode with editingStyle UITableViewCellEditingStyleDelete it indents the cell a little and can push the content out some causing the rowHeight to change. 当单元格使用editingStyle UITableViewCellEditingStyleDelete进入编辑模式时,它将使该单元格缩进一点,并且可以将内容压出一些,从而导致rowHeight发生变化。 Again this works well as switching the tableview to editing causes the table to relayout and therefor the rowHeight gets recalculated. 再次将表格视图切换为编辑会导致表格重新布局,因此可以重新计算rowHeight。

When you click the red button or swipe the cell a Delete button appears on right hand side of the cell. 当您单击红色按钮或滑动该单元格时,该单元格右侧会出现一个删除按钮。 This however does NOT trigger a relayout of the table, only the cell itself. 但是,这不会触发表的重新布局,只会触发单元格本身。 The problem here is that if the content goes over the bottom edge the cell does not get resized. 这里的问题是,如果内容超出底部边缘,则单元格不会调整大小。

Is there a way to trigger a re-layout of the table when the delete button appears? 当出现删除按钮时,是否有办法触发表格的重新布局?

In general, height of UITableViewCell can be only setup via tableview:heightForRowAtIndexPath: , and the latter method only be called once (per row per section) before tableView:cellForRowAtIndexPath: , so you have to [tableview reloadData] if want to adjust the UITableViewCell height. 通常,只能通过tableview:heightForRowAtIndexPath:设置UITableViewCell高度,而后者方法在tableView:cellForRowAtIndexPath:之前只能被调用一次(每节每行一次),因此如果要调整UITableViewCell,则必须使用[tableview reloadData]高度。

Maybe your can do something in these delegate methods 也许您可以在这些委托方法中做一些事情

// The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath; 

To your question, you could reset the properties of the text control at - (void)layoutSubviews in your custom UITableViewCell. 对于您的问题,您可以在自定义UITableViewCell中的- (void)layoutSubviews处重置文本控件的属性。

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat theWidth = self.frame.size.height; //changed when entering the edit style
    ...
}

Hope these will help you. 希望这些对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM