UITableViewCellã«UITextFieldãåãã
ããã¼ãã«ãã¥ã¼ã使ã£ããµã³ãã«ãä½ããã·ãªã¼ãºã¯é¢åã«ãªã£ããã§ããã
Guildã¿ã°ã§ãããããã¾ãããããªæãã§ã
ä½æ¥å 容
ãã¼ãã«ãã¥ã¼ã®ã»ã«ã«ããã¹ããã£ã¼ã«ããåãè¾¼ãã
-
- ãã¾ã
- ãã¼ãã¼ããèªå表示ã
- ããã¹ãå ¥åä¸ãããã¹ããã£ã¼ã«ãã®å³å´ã«ï¼¸ãã¿ã³ã表示ã
- ãã¾ã
Xcode
Xcodeã§UITableViewCellã®ç¶æ¿ã¯ã©ã¹ãä½æã
ã«ã¹ã¿ã ã»ã«
- æ°è¦ãã¡ã¤ã«ã§ãUITableViewCell Subclass ãã³ãã¬ã¼ããããEditableCell ã¯ã©ã¹ã追å ã
- EditableCellã¯ã©ã¹ã«UITextFieldã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã追å ã
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) { // Initialization code textField = [[UITextField alloc] initWithFrame:CGRectZero]; textField.font = [UIFont systemFontOfSize:20.0]; textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // TextFieldã®å³å´ã«ï¼¸ãã¿ã³ã表示 textField.clearButtonMode = UITextFieldViewModeWhileEditing; // ããã¹ããã£ã¼ã«ãããã¼ãã«ãã¥ã¼ã»ã«ã«è¿½å [self addSubview:textField]; } return self; } - (void)layoutSubviews { // CGRectInset : ä¸å¤®ãåºå®ãããªãã»ããã§dx,dyãæå®ãã // ãã®å ´åãã¨ã®ãµã¤ãºãããå°ãããªã textField.frame = CGRectInset(self.contentView.bounds, 20, 0); }
UITableViewDatasourceãããã³ã«ã®tableView:cellForRowAtIndexPath:ã§ã«ã¹ã¿ã ã»ã«ãè¿ãããã«ããã
Controllerã¯ã©ã¹
EditableCellã®ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã追å ããã¤ã³ã¹ã¿ã³ã¹åããã
- (void)viewDidLoad { . . . . if (nameCell == nil) { nameCell = [[EditableCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"nameCell"]; } [nameCell.textField setPlaceholder:@"character name"]; }tableView:cellForRowAtIndexPath:ã§ãã«ã¹ã¿ã ã»ã«ãè¿ãã
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { . . . . if (indexPath.section == 0) { return nameCell; } else { cell.text = @"種æã¨æ§å¥ãé¸æãã¦ãã ãã"; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.selectionStyle = UITableViewCellSelectionStyleNone; } return cell; }ãã¥ã¼è¡¨ç¤ºæã«èªåçã«ãã¼ãã¼ãã表示ãããããã«ããã
ã«ã¹ã¿ã ã»ã«ããã¡ã¼ã¹ãã¬ã¹ãã³ãã«ãããã¨ã§ãã¼ãã¼ãã表示ã§ãã- (void)viewWillAppear:(BOOL)animated { [nameCell.textField becomeFirstResponder]; } - (void)viewWillDisappear:(BOOL)animated { // ãããªããããããªããã© [nameCell.textField resignFirstResponder]; }
次ã®äºå®
ããã«ã¼ãå®è£ ã