Skip to content

Commit 6512f1f

Browse files
committed
Add row insertion and deletion functionality to sample
1 parent 634ddbf commit 6512f1f

3 files changed

Lines changed: 77 additions & 4 deletions

File tree

TableViewCellWithAutoLayout/TableViewController/RJModel.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828

2929
@interface RJModel : NSObject
3030

31-
@property (nonatomic, strong) NSArray *dataSource;
31+
@property (nonatomic, strong) NSMutableArray *dataSource;
3232

3333
- (void)populateDataSource;
3434

35+
- (void)addSingleItemToDataSource;
36+
3537
@end

TableViewCellWithAutoLayout/TableViewController/RJModel.m

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@ - (id)init {
3636
return self;
3737
}
3838

39+
- (NSMutableArray *)dataSource
40+
{
41+
if (!_dataSource) {
42+
_dataSource = [NSMutableArray new];
43+
}
44+
return _dataSource;
45+
}
46+
3947
- (void)populateDataSource {
4048

4149
NSArray *fontFamilies = [NSArray arrayWithArray:[UIFont familyNames]];
4250
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:[fontFamilies count]];
43-
51+
4452
for ( NSString *familyName in fontFamilies ) {
4553
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:familyName, @"title",
4654
[self randomLorumIpsum], @"body",
@@ -51,7 +59,25 @@ - (void)populateDataSource {
5159
[result addObject:dictionary];
5260
}
5361

54-
self.dataSource = [NSArray arrayWithArray:result];
62+
self.dataSource = result;
63+
64+
}
65+
66+
- (void)addSingleItemToDataSource {
67+
68+
NSArray *fontFamilies = [NSArray arrayWithArray:[UIFont familyNames]];
69+
70+
int r = arc4random() % [fontFamilies count];
71+
NSString *familyName = [fontFamilies objectAtIndex:r];
72+
73+
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:familyName, @"title",
74+
[self randomLorumIpsum], @"body",
75+
@"", @"element2",
76+
@"", @"element3",
77+
@"", @"element4",
78+
nil];
79+
80+
[self.dataSource addObject:dictionary];
5581

5682
}
5783

TableViewCellWithAutoLayout/TableViewController/RJTableViewController.m

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@
3131
static 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

Comments
 (0)