3131static NSString *CellIdentifier = @" CellIdentifier" ;
3232
3333@interface RJTableViewController ()
34+
3435@property (strong , nonatomic ) RJModel *model;
36+
37+ // This property is used to work around the constraint exception that is thrown if the
38+ // estimated row height for an inserted row is greater than the actual height for that row.
39+ // See: https://github.com/caoimghgin/TableViewCellWithAutoLayout/issues/6
40+ @property (assign , nonatomic ) BOOL isInsertingRow;
41+
3542@end
3643
3744@implementation RJTableViewController
@@ -53,6 +60,10 @@ - (void)viewDidLoad
5360 [super viewDidLoad ];
5461
5562 [self .tableView registerClass: [RJTableViewCell class ] forCellReuseIdentifier: CellIdentifier];
63+
64+ self.navigationItem .leftBarButtonItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemTrash target: self action: @selector (clear: )];
65+
66+ self.navigationItem .rightBarButtonItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target: self action: @selector (addRow: )];
5667}
5768
5869- (void )viewDidAppear : (BOOL )animated
@@ -85,6 +96,32 @@ - (void)contentSizeCategoryChanged:(NSNotification *)notification
8596 [self .tableView reloadData ];
8697}
8798
99+ - (void )clear : (id )sender
100+ {
101+ NSMutableArray *rowsToDelete = [NSMutableArray new ];
102+ for (NSUInteger i = 0 ; i < [self .model.dataSource count ]; i++) {
103+ [rowsToDelete addObject: [NSIndexPath indexPathForRow: i inSection: 0 ]];
104+ }
105+
106+ self.model = [[RJModel alloc ] init ];
107+
108+ [self .tableView deleteRowsAtIndexPaths: rowsToDelete withRowAnimation: UITableViewRowAnimationAutomatic];
109+
110+ [self .tableView reloadData ];
111+ }
112+
113+ - (void )addRow : (id )sender
114+ {
115+ [self .model addSingleItemToDataSource ];
116+
117+ self.isInsertingRow = YES ;
118+
119+ NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow: [self .model.dataSource count ] - 1 inSection: 0 ];
120+ [self .tableView insertRowsAtIndexPaths: @[lastIndexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
121+
122+ self.isInsertingRow = NO ;
123+ }
124+
88125#pragma mark - Table view data source
89126
90127- (NSInteger )numberOfSectionsInTableView : (UITableView *)tableView
@@ -143,7 +180,15 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
143180
144181- (CGFloat)tableView : (UITableView *)tableView estimatedHeightForRowAtIndexPath : (NSIndexPath *)indexPath
145182{
146- return 500 .0f ;
183+ if (self.isInsertingRow ) {
184+ // A constraint exception will be thrown if the estimated row height for an inserted row is greater
185+ // than the actual height for that row. In order to work around this, we return the actual height
186+ // for the the row when inserting into the table view.
187+ // See: https://github.com/caoimghgin/TableViewCellWithAutoLayout/issues/6
188+ return [self tableView: tableView heightForRowAtIndexPath: indexPath];
189+ } else {
190+ return 500 .0f ;
191+ }
147192}
148193
149194@end
0 commit comments