在UITableViewCell中有一个UITextField

在UITableViewCell中有一个UITextField

我现在试图这样做几天,在阅读了大量试图这样做的人的消息之后,我仍然无法UITextField在我的一些人中完全工作UITableViewCells,就像在这个例子中一样:

要么我的表单工作,但文本不可见(虽然我将其颜色设置为蓝色),当我点击它时键盘在场上,我无法正确实现键盘事件。我尝试了一些来自Apple的例子(主要是UICatalog,有一个类似的控件),但它仍然无法正常工作。

有人可以帮我(和所有的人试图实现这种控制)和后一个简单实现的UITextField一个UITableViewCell,工作正常?


翻阅古今
浏览 705回答 3
3回答

慕妹3146593

试试吧。对我来说就像一个魅力(在iPhone设备上)。我将此代码用于登录屏幕一次。我将表视图配置为包含两个部分。你当然可以摆脱部分条件。- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];if (cell == nil) {     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault                                     reuseIdentifier:kCellIdentifier] autorelease];     cell.accessoryType = UITableViewCellAccessoryNone;     if ([indexPath section] == 0) {         UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];         playerTextField.adjustsFontSizeToFitWidth = YES;         playerTextField.textColor = [UIColor blackColor];         if ([indexPath row] == 0) {             playerTextField.placeholder = @"example@gmail.com";             playerTextField.keyboardType = UIKeyboardTypeEmailAddress;             playerTextField.returnKeyType = UIReturnKeyNext;         }         else {             playerTextField.placeholder = @"Required";             playerTextField.keyboardType = UIKeyboardTypeDefault;             playerTextField.returnKeyType = UIReturnKeyDone;             playerTextField.secureTextEntry = YES;         }                playerTextField.backgroundColor = [UIColor whiteColor];         playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support         playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support         playerTextField.textAlignment = UITextAlignmentLeft;         playerTextField.tag = 0;         //playerTextField.delegate = self;         playerTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right         [playerTextField setEnabled: YES];         [cell.contentView addSubview:playerTextField];         [playerTextField release];     }}if ([indexPath section] == 0) { // Email & Password Section     if ([indexPath row] == 0) { // Email         cell.textLabel.text = @"Email";     }     else {         cell.textLabel.text = @"Password";     }}else { // Login button section     cell.textLabel.text = @"Log in";}return cell;    }

30秒到达战场

以下是我如何实现这一目标:TextFormCell.h#import&nbsp;<UIKit/UIKit.h>#define&nbsp;CellTextFieldWidth&nbsp;90.0#define&nbsp;MarginBetweenControls&nbsp;20.0@interface&nbsp;TextFormCell&nbsp;:&nbsp;UITableViewCell&nbsp;{ &nbsp;UITextField&nbsp;*textField;}@property&nbsp;(nonatomic,&nbsp;retain)&nbsp;UITextField&nbsp;*textField;@endTextFormCell.m#import&nbsp;"TextFormCell.h"@implementation&nbsp;TextFormCell@synthesize&nbsp;textField;-&nbsp;(id)initWithReuseIdentifier:(NSString&nbsp;*)reuseIdentifier&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(self&nbsp;=&nbsp;[super&nbsp;initWithReuseIdentifier:reuseIdentifier])&nbsp;{ &nbsp;&nbsp;//&nbsp;Adding&nbsp;the&nbsp;text&nbsp;field &nbsp;&nbsp;textField&nbsp;=&nbsp;[[UITextField&nbsp;alloc]&nbsp;initWithFrame:CGRectZero]; &nbsp;&nbsp;textField.clearsOnBeginEditing&nbsp;=&nbsp;NO; &nbsp;&nbsp;textField.textAlignment&nbsp;=&nbsp;UITextAlignmentRight; &nbsp;&nbsp;textField.returnKeyType&nbsp;=&nbsp;UIReturnKeyDone; &nbsp;&nbsp;[self.contentView&nbsp;addSubview:textField]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self;}-&nbsp;(void)dealloc&nbsp;{ &nbsp;[textField&nbsp;release]; &nbsp;&nbsp;&nbsp;&nbsp;[super&nbsp;dealloc];}#pragma&nbsp;mark&nbsp;-#pragma&nbsp;mark&nbsp;Laying&nbsp;out&nbsp;subviews-&nbsp;(void)layoutSubviews&nbsp;{ &nbsp;CGRect&nbsp;rect&nbsp;=&nbsp;CGRectMake(self.contentView.bounds.size.width&nbsp;-&nbsp;5.0,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12.0,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-CellTextFieldWidth,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;25.0); &nbsp;[textField&nbsp;setFrame:rect]; &nbsp;CGRect&nbsp;rect2&nbsp;=&nbsp;CGRectMake(MarginBetweenControls, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12.0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.contentView.bounds.size.width&nbsp;-&nbsp;CellTextFieldWidth&nbsp;-&nbsp;MarginBetweenControls, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;25.0); &nbsp;UILabel&nbsp;*theTextLabel&nbsp;=&nbsp;(UILabel&nbsp;*)[self&nbsp;textLabel]; &nbsp;[theTextLabel&nbsp;setFrame:rect2];}它可能看起来有点冗长,但它确实有效!别忘了设置代表!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS