1

I add a datetime label on the right of a table cell. When swip-to-delete shows the "Delete" button, the datetime label need to shift left a little bit. But how to get the "Delete" button's size?

I tried to find it in the cell.subviews but failed.

al_lea
  • 596
  • 1
  • 9
  • 17

5 Answers5

3

You don't have to know the button's size. Instead, use the size of the cell's contentView property to calculate the sizes of the subviews. When swiping over a cell, UIKit will adapt the contentView's size and call layoutSubviews on the cell-object. In your subclass of UITableViewCell, overwrite the layoutSubviews method and set the appropriate sizes to the subviews.

Look at RecipeTableViewCell.m of Apple's iPhoneCoreDataRecipes sample code.

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
fjoachim
  • 906
  • 6
  • 11
2

Use this code in your custom Cell class

 - (void)layoutSubviews {
        [super layoutSubviews];
        NSMutableArray *subviews = [self.subviews mutableCopy];
        while (subviews.count > 0)
        {
            UIView *subV = subviews[0];
            [subviews removeObjectAtIndex:0];
            if ([NSStringFromClass([subV class])isEqualToString:@"UITableViewCellDeleteConfirmationView"]) 
           {
            UIView *deleteButtonView = (UIView *)[self.subviews objectAtIndex:0];
            CGFloat  deleteBtnHeight=deleteButtonView.frame.size.height;//here you get the height
             }
        }
    }
Arun Basil Issac
  • 275
  • 2
  • 10
1

The size adjusts to fit the text contained. See the following code:

- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"Dynamic width!";
}

vs

- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"⏎";
}
Taylor
  • 240
  • 1
  • 10
0

If you don't override layoutSubviews method in your custom table view cell than my approach is:

  1. Create your custom subview, set frame basing on contentView.bounds.
  2. Set autoresizingMask to UIViewAutoresizingFlexibleWidth.
  3. Add your custom subview to ContentView of a cell.
  4. Configure cell for editing

Now when you swipe on cell the delete button appears and your view auto resizes with contentView.

Community
  • 1
  • 1
DanSkeel
  • 3,853
  • 35
  • 54
-1

The delete button is 63x33.

Ray Wenderlich
  • 480
  • 2
  • 8