ios - UITableView 删除行减少表格行高

标签 ios uitableview

当我从我的表格 View 中删除一行时,剩余行的高度会减少大约 20 点/像素/无论 Apple 表格行的测量值。当我第一次显示表格时,该行适合内容 - 我配置内容这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Set up the cell...
    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    Favorite *thisFavorite = [self.arrResults objectAtIndex:[indexPath row]];

    NSMutableAttributedString* strAtttributedText;

    // Define general attributes for the entire text
    NSDictionary *attribs = @{
                              NSForegroundColorAttributeName: cell.textLabel.textColor,
                              NSFontAttributeName: cell.textLabel.font
                              };
    NSString* strCellText = [NSString stringWithFormat:@"%@\n%@\n%@", thisFavorite.favName, thisFavorite.favAddress, thisFavorite.favCity];


    //get location of first return TO DO - need to figure out how to return the range from the string above
    NSRange newLineRange = [strCellText rangeOfString: @"\n"];
    NSRange firstLineRange = NSMakeRange(0, newLineRange.location);
    NSRange restOfTextRange = NSMakeRange(newLineRange.location + 1, strCellText.length-newLineRange.location-1);

    strAtttributedText = [[NSMutableAttributedString alloc] initWithString:strCellText attributes:attribs];


    [strAtttributedText setAttributes:@{NSForegroundColorAttributeName:MPL_BLUE} range:firstLineRange];

    [strAtttributedText setAttributes:@{NSForegroundColorAttributeName:MPL_LIGHTGRAY, NSFontAttributeName:TABLE_CELL_FONT} range:restOfTextRange];

    cell.textLabel.attributedText = strAtttributedText;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;

}

我正在以这种方式删除行:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        Favorite* thisFavorite = [self.arrResults objectAtIndex:indexPath.row];

        [self.tableView beginUpdates];

        NSArray* arrIndexPaths = [NSArray arrayWithObjects:indexPath, nil];
        [self.tableView deleteRowsAtIndexPaths:arrIndexPaths withRowAnimation:UITableViewRowAnimationFade];
        [self.arrResults removeObjectAtIndex:indexPath.row];

        [self.tableView endUpdates];

        [self.myController deleteManagedObject:thisFavorite];

    }
}

在此过程中,我应该在哪里管理单元格高度?

我可以从这里获取初始单元格框架(它们都是相同的内容样式 - name\naddress\ncity,state,zip):

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    self.cellSize = cell.frame;
}

我尝试在开始和结束编辑之间删除 self.tableview.rowHeight = self.cellSize.size.height 但它没有任何影响。

如有任何帮助,我们将不胜感激。

最佳答案

你应该实现这个方法并为你的单元格返回一个恒定的高度(我没有在你发布的代码片段中看到它):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return someConstantIntValue;
}

关于ios - UITableView 删除行减少表格行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32355962/

相关文章:

ios - 拒绝二进制文件后重新提交应用内购买。

ios - 如何从 iOS7 中的 UIWindow 对象中获取 UIAlertView

ios - 为 iPhone 和 iPad 布局静态表格单元格

iphone - 如何从选定的 UITableViewCell 获取自定义 NSManagedObject 子类

ios - 使用 UIAppearance 代理自定义 UITableViewCell

ios - 为什么这个 View 会神奇地改变大小?

ios - NSInteger 乘法 : Invalid operands to binary expression

ios - Swift 4 UITableView 单元格高度不起作用

ios - topLayoutGuide 在 vi​​ewWillAppear 之后应用

ios - 将现有数组绑定(bind)到 tableView IOS