Last active
February 19, 2018 20:23
-
-
Save DonMag/46d529793b066de803bd9a59a06a2de2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AutoSizeTableViewCell.m | |
// OCVeryTemp | |
// | |
// Created by Don Mag on 2/19/18. | |
// | |
#import "AutoSizeTableViewCell.h" | |
@implementation AutoSizeTableViewCell | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// WithAlertTableViewController.h | |
// OCVeryTemp | |
// | |
// Created by Don Mag on 2/19/18. | |
// | |
#import <UIKit/UIKit.h> | |
@interface WithAlertTableViewController : UITableViewController | |
@end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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