Last active
August 24, 2016 15:23
-
-
Save andkon/8571788 to your computer and use it in GitHub Desktop.
Custom cell with UITextField - for use with storyboards
This file contains hidden or 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
Storyboard configuration: | |
1. Make a cell. | |
2. Give it a custom class of YourCell | |
3. Drag a label onto the cell, give the label a tag of 100. Configure and arrange as necessary. | |
4. Drag a text field onto the cell, give the textField a tag of 101. Configure and arrange as necessary. |
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
@interface ENVNeedsCell : UITableViewCell | |
@property (weak, nonatomic) IBOutlet UITextField *textField; | |
@property (weak, nonatomic) IBOutlet UILabel *label; | |
@end |
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
// Don't forget the text field delegate here! | |
@interface ENVNeedsViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> | |
@property (strong, nonatomic) NSMutableDictionary *dictOfLabelsAndText; | |
@end |
This file contains hidden or 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
#import "ENVNeedsViewController.h" | |
#import "ENVNeedsCell.h" | |
@implementation ENVNeedsViewController | |
- (id)initWithStyle:(UITableViewStyle)style | |
{ | |
self = [super initWithStyle:style]; | |
if (self) { | |
// Custom initialization | |
} | |
return self; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Here's where you'd load the data to populate the cell labels and text | |
// You should also initialize the dictOfLabelsAndText here too. | |
self.dictOfLabelsAndText = [[NSMutableDictionary alloc] init]; | |
self.clearsSelectionOnViewWillAppear = NO; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
#pragma mark - Table view data source | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
// Return the number of rows in the section. | |
NSArray *cellTitles = // Make an ordered array of strings for the cell titles. | |
return [cellTitles count]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
// Create a cell & register it for dequeing | |
static NSString *CellIdentifier = @"Cell"; | |
YouCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; | |
// Set the cell's label: | |
cell.label.text = cellTitles[indexPath.row]; | |
// Prepping the text field | |
// 1. Set the text field's delegate to be this view controller | |
// 2. Find out if there's an existing string for it to display in the self.dictOfLabelsAndText. If not, set it to be empty. | |
cell.textField.delegate = self; | |
NSString *key = cell.label.text; | |
if ([self.dictOfLabelsAndText valueForKey:key] != nil) { | |
cell.textField.text = [self.dictOfLabelsAndText valueForKey:cell.needTitle.text]; | |
} else { | |
cell.textField.text = @""; | |
} | |
return cell; | |
} | |
// Here we save the textField's string whenever the user finishes editing it. | |
- (void)textFieldDidEndEditing:(UITextField *)textField | |
{ | |
// Make sure to set the label to have a ta | |
UILabel *label = (UILabel *)[textField.superview viewWithTag:100]; | |
NSString *labelString = label.text; | |
NSString *textFieldString = textField.text; | |
[self.dictOfLabelsAndText setObject:textFieldString forKey:labelString]; | |
} | |
#pragma mark - | |
#pragma mark Table View Delegate Methods | |
- (void)tableView:(UITableView *)tableView | |
didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
// Whenever you click a cell, bring up a keyboard to edit the textField | |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; | |
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; | |
UITextField *textField = (UITextField *)[cell viewWithTag:101]; | |
[textField becomeFirstResponder]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment