Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView deleting row reduced table row height

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.

like image 813
PruitIgoe Avatar asked Dec 05 '25 03:12

PruitIgoe


1 Answers

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;
}
like image 122
Rony Rozen Avatar answered Dec 07 '25 16:12

Rony Rozen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!