📄 textfieldcontroller.m
字号:
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleNone;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3;}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ NSString *title; switch (section) { case kUITextField_Section: { title = @"UITextField"; break; } case kUITextField_Rounded_Custom_Section: { title = @"UITextField Rounded"; break; } case kUITextField_Secure_Section: { title = @"UITextField Secure"; break; } } return title;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 2;}// to determine specific row height for each cell, override this. In this example, each row is determined// buy the its subviews that are embedded.//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ CGFloat result; switch ([indexPath row]) { case 0: { result = kUIRowHeight; break; } case 1: { result = kUIRowLabelHeight; break; } } return result;}// utility routine leveraged by 'cellForRowAtIndexPath' to determine which UITableViewCell to be used on a given row//- (UITableViewCell *)obtainTableCellForRow:(NSInteger)row{ UITableViewCell *cell = nil; if (row == 0) cell = [myTableView dequeueReusableCellWithIdentifier:kCellTextField_ID]; else if (row == 1) cell = [myTableView dequeueReusableCellWithIdentifier:kSourceCell_ID]; if (cell == nil) { if (row == 0) { cell = [[[CellTextField alloc] initWithFrame:CGRectZero reuseIdentifier:kCellTextField_ID] autorelease]; ((CellTextField *)cell).delegate = self; // so we can detect when cell editing starts } else if (row == 1) { cell = [[[SourceCell alloc] initWithFrame:CGRectZero reuseIdentifier:kSourceCell_ID] autorelease]; } } return cell;}// to determine which UITableViewCell to be used on a given row.//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSInteger row = [indexPath row]; UITableViewCell *sourceCell = [self obtainTableCellForRow:row]; switch (indexPath.section) { case kUITextField_Section: { if (row == 0) { // this cell hosts the text field control ((CellTextField *)sourceCell).view = textField; } else { // this cell hosts the info on where to find the code ((SourceCell *)sourceCell).sourceLabel.text = @"TextFieldController.m - createTextField"; } textFieldCell = (CellTextField *)sourceCell; // kept track for editing break; } case kUITextField_Rounded_Custom_Section: { if (row == 0) { // this cell hosts the rounded text field control ((CellTextField *)sourceCell).view = textFieldRounded; } else { // this cell hosts the info on where to find the code ((SourceCell *)sourceCell).sourceLabel.text = @"TextFieldController.m - createTextField_Rounded"; } textFieldRoundedCell = (CellTextField *)sourceCell; // kept track for editing break; } case kUITextField_Secure_Section: { // we are creating a new cell, setup its attributes if (row == 0) { // this cell hosts the secure text field control ((CellTextField *)sourceCell).view = textFieldSecure; } else { // this cell hosts the info on where to find the code ((SourceCell *)sourceCell).sourceLabel.text = @"TextFieldController.m - createTextField_Secure"; } textFieldSecureCell = (CellTextField *)sourceCell; // kept track for editing break; } } return sourceCell;}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 37.0;}#pragma mark -#pragma mark <EditableTableViewCellDelegate> Methods and editing management- (BOOL)cellShouldBeginEditing:(EditableTableViewCell *)cell{ // notify other cells to end editing if (![cell isEqual:textFieldCell]) [textFieldCell stopEditing]; if (![cell isEqual:textFieldRoundedCell]) [textFieldRoundedCell stopEditing]; if (![cell isEqual:textFieldSecureCell]) [textFieldSecureCell stopEditing]; return self.editing;}- (void)cellDidEndEditing:(EditableTableViewCell *)cell{ if ([cell isEqual:textFieldSecureCell] || [cell isEqual:textFieldRoundedCell]) { // Restore the position of the main view if it was animated to make room for the keyboard. if (self.view.frame.origin.y < 0) { [self setViewMovedUp:NO]; } }}// Animate the entire view up or down, to prevent the keyboard from covering the author field.- (void)setViewMovedUp:(BOOL)movedUp{ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; // Make changes to the view's frame inside the animation block. They will be animated instead // of taking place immediately. CGRect rect = self.view.frame; if (movedUp) { // If moving up, not only decrease the origin but increase the height so the view // covers the entire screen behind the keyboard. rect.origin.y -= kOFFSET_FOR_KEYBOARD; rect.size.height += kOFFSET_FOR_KEYBOARD; } else { // If moving down, not only increase the origin but decrease the height. rect.origin.y += kOFFSET_FOR_KEYBOARD; rect.size.height -= kOFFSET_FOR_KEYBOARD; } self.view.frame = rect; [UIView commitAnimations];}- (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:animated]; if (!editing) { [textFieldCell stopEditing]; [textFieldRoundedCell stopEditing]; [textFieldSecureCell stopEditing]; }}- (void)keyboardWillShow:(NSNotification *)notif{ // The keyboard will be shown. If the user is editing the author, adjust the display so that the // author field will not be covered by the keyboard. if ((textFieldRoundedCell.isInlineEditing || textFieldSecureCell.isInlineEditing) && self.view.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } else if (!textFieldSecureCell.isInlineEditing && self.view.frame.origin.y < 0) { [self setViewMovedUp:NO]; }}#pragma mark - UIViewController delegate methods- (void)viewWillAppear:(BOOL)animated{ // watch the keyboard so we can adjust the user interface if necessary. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window]; }- (void)viewWillDisappear:(BOOL)animated{ [self setEditing:NO animated:YES]; // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; }@end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -