3

When I delete a row from my tableview the remaining rows height get's reduced by about 20 or so points/pixels/whatever Apple table rows are measured in. When I first display the table the row fits the content - I configure the content this way:

- (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;

}

and I am deleting the row this way:

- (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];

    }
}

where would I manage the cell height in this process?

I can get the initial cell frame (they are all the same content style - name\naddress\ncity,state,zip) from here:

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

    self.cellSize = cell.frame;
}

I tried dropping self.tableview.rowHeight = self.cellSize.size.height inbetween begin and end editing but it had no affect.

Any help would be appreciated.

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137

1 Answers1

1

You should implement this method and return a constant height for your cells (I didn't see it in the code snippet you posted):

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return someConstantIntValue;
}
Rony Rozen
  • 3,957
  • 4
  • 24
  • 46
  • That did it. But I'd still like to know how come when the tableview is first seen the cells all adjust to the height of the content but once you edit one row the cells reduce in size? – PruitIgoe Sep 02 '15 at 16:54