Skip to content

Instantly share code, notes, and snippets.

@DonMag
Last active February 19, 2018 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonMag/46d529793b066de803bd9a59a06a2de2 to your computer and use it in GitHub Desktop.
Save DonMag/46d529793b066de803bd9a59a06a2de2 to your computer and use it in GitHub Desktop.
//
// AutoSizeTableViewCell.h
// OCVeryTemp
//
// Created by Don Mag on 2/19/18.
//
#import <UIKit/UIKit.h>
@interface AutoSizeTableViewCell : UITableViewCell
@property IBOutlet UILabel *topLabel;
@property IBOutlet UILabel *botLabel;
@end
//
// AutoSizeTableViewCell.m
// OCVeryTemp
//
// Created by Don Mag on 2/19/18.
//
#import "AutoSizeTableViewCell.h"
@implementation AutoSizeTableViewCell
@end
//
// WithAlertTableViewController.h
// OCVeryTemp
//
// Created by Don Mag on 2/19/18.
//
#import <UIKit/UIKit.h>
@interface WithAlertTableViewController : UITableViewController
@end
//
// WithAlertTableViewController.m
// OCVeryTemp
//
// Created by Don Mag on 2/19/18.
//
#import "WithAlertTableViewController.h"
#import "AutoSizeTableViewCell.h"
@interface WithAlertTableViewController ()
@property NSMutableArray *theData;
@end
@implementation WithAlertTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// init data array with single-letter strings
_theData = [NSMutableArray array];
NSString *str = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < [str length]; i++) {
NSString *ch = [str substringWithRange:NSMakeRange(i, 1)];
[_theData addObject:ch];
}
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80.0;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_theData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AutoSizeTableViewCell *cell = (AutoSizeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"AutoCell" forIndexPath:indexPath];
cell.topLabel.text = [NSString stringWithFormat:@"Row: %ld - %@", (long)indexPath.row, _theData[indexPath.row]];
// set bottom label to 1, 2 or 3 lines of text
NSString *bottomText = @"Bottom label text";
for (int i = 0; i < indexPath.row % 3; i++) {
bottomText = [NSString stringWithFormat:@"%@\nLine %ld", bottomText, (long)i];
}
cell.botLabel.text = bottomText;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *title = @"This is the title";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
// add a line of text to the data for the selected row
_theData[indexPath.row] = [NSString stringWithFormat:@"%@\n%@", _theData[indexPath.row], @"Added Line of text"];
// save
[alertController addAction:[UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// this causes a scroll jump when called in this block
// (but I don't see a "jump" in this test)
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}]];
// cancel
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment