在UITableViewCell中有一個UITextField我現(xiàn)在試圖這樣做幾天,在閱讀了大量試圖這樣做的人的消息之后,我仍然無法UITextField在我的一些人中完全工作UITableViewCells,就像在這個例子中一樣:要么我的表單工作,但文本不可見(雖然我將其顏色設(shè)置為藍色),當(dāng)我點擊它時鍵盤在場上,我無法正確實現(xiàn)鍵盤事件。我嘗試了一些來自Apple的例子(主要是UICatalog,有一個類似的控件),但它仍然無法正常工作。有人可以幫我(和所有的人試圖實現(xiàn)這種控制)和后一個簡單實現(xiàn)的UITextField一個UITableViewCell,工作正常?
3 回答

慕妹3146593
TA貢獻1820條經(jīng)驗 獲得超9個贊
試試吧。對我來說就像一個魅力(在iPhone設(shè)備上)。我將此代碼用于登錄屏幕一次。我將表視圖配置為包含兩個部分。你當(dāng)然可以擺脫部分條件。
- (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秒到達戰(zhàn)場
TA貢獻1828條經(jīng)驗 獲得超6個贊
以下是我如何實現(xiàn)這一目標(biāo):
TextFormCell.h
#import <UIKit/UIKit.h>#define CellTextFieldWidth 90.0#define MarginBetweenControls 20.0@interface TextFormCell : UITableViewCell { UITextField *textField;}@property (nonatomic, retain) UITextField *textField;@end
TextFormCell.m
#import "TextFormCell.h"@implementation TextFormCell@synthesize textField;- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithReuseIdentifier:reuseIdentifier]) { // Adding the text field textField = [[UITextField alloc] initWithFrame:CGRectZero]; textField.clearsOnBeginEditing = NO; textField.textAlignment = UITextAlignmentRight; textField.returnKeyType = UIReturnKeyDone; [self.contentView addSubview:textField]; } return self;}- (void)dealloc { [textField release]; [super dealloc];}#pragma mark -#pragma mark Laying out subviews- (void)layoutSubviews { CGRect rect = CGRectMake(self.contentView.bounds.size.width - 5.0, 12.0, -CellTextFieldWidth, 25.0); [textField setFrame:rect]; CGRect rect2 = CGRectMake(MarginBetweenControls, 12.0, self.contentView.bounds.size.width - CellTextFieldWidth - MarginBetweenControls, 25.0); UILabel *theTextLabel = (UILabel *)[self textLabel]; [theTextLabel setFrame:rect2];}
它可能看起來有點冗長,但它確實有效!
別忘了設(shè)置代表!
- 3 回答
- 0 關(guān)注
- 723 瀏覽
添加回答
舉報
0/150
提交
取消