简体   繁体   English

从UITableView删除单元格时调用heightForRowAtIndexPath

[英]Calling heightForRowAtIndexPath when deleting cells from UITableView

I am calling heightForRowAtIndexPath to adjust my cells. 我正在调用heightForRowAtIndexPath来调整我的单元格。 When I edit my table view to delete cells the height of the cells stay fixed so the cells overlap which creates not a very appealing image. 当我编辑表格视图以删除单元格时,单元格的高度保持固定,因此单元格重叠,从而生成的图像不太吸引人。 I would like to know how to change the height of the cell when deleting of the cells begins. 我想知道在删除单元格时如何更改单元格的高度。

This is my current code 这是我当前的代码

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

Favorites *favorite = [fetchedResultsController objectAtIndexPath:indexPath];
NSString *cellText = favorite.name;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];

CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);

CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

return labelSize.height + 20;

} }

Any help will be appreciated! 任何帮助将不胜感激! Thanks 谢谢

- (void)tableView:(UITableView *)tableView commitEditingStyle: UITableViewCellEditingStyle)editingStyle 
       forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
       [tableView reloadTable];
    }   
}

and you can change a property which will tell u want the right height, and read its value from heightForRowAtIndexPath 并且您可以更改一个属性,该属性将告诉您想要合适的高度,并从heightForRowAtIndexPath中读取其值

Your problem is that if numberOfLines is 0 for a label in your cell (which it probably is so that your cell can expand), then the label will expand again when delete slides in, causing the ugly overlap. 您的问题是,如果单元格中的标签的numberOfLines为0(可能是这样,以便您的单元格可以展开),那么当删除幻灯片插入时,标签将再次展开,从而导致难看的重叠。 A worthy alternative is to truncate the text when the delete slide comes in. To do this you need to set the number of lines when you make the cell. 一个有价值的替代方法是在插入删除幻灯片时截断文本。为此,您需要在制作单元格时设置行数。

cell.textLabel.numberOfLines = textLabelheight/textLabelFontSize;

You probably already know textLabelHeight and textLabelFontSize, cos you worked it out when you adjusted the cell height. 您可能已经知道textLabelHeight和textLabelFontSize,这是因为您在调整单元格高度时已经解决了。

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

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