UITableView删除行减少了表格行高

时间:2015-09-02 14:30:31

标签: ios uitableview

当我从tableview中删除一行时,剩下的行高度减少了大约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,但它没有任何影响。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

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

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