在UITableViewCell中有一个UITextField我现在试图这样做几天,在阅读了大量试图这样做的人的消息之后,我仍然无法UITextField在我的一些人中完全工作UITableViewCells,就像在这个例子中一样:要么我的表单工作,但文本不可见(虽然我将其颜色设置为蓝色),当我点击它时键盘在场上,我无法正确实现键盘事件。我尝试了一些来自Apple的例子(主要是UICatalog,有一个类似的控件),但它仍然无法正常工作。有人可以帮我(和所有的人试图实现这种控制)和后一个简单实现的UITextField一个UITableViewCell,工作正常?
3 回答
慕妹3146593
TA贡献1820条经验 获得超9个赞
试试吧。对我来说就像一个魅力(在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秒到达战场
TA贡献1828条经验 获得超6个赞
以下是我如何实现这一目标:
TextFormCell.h
#import <UIKit/UIKit.h>#define CellTextFieldWidth 90.0#define MarginBetweenControls 20.0@interface TextFormCell : UITableViewCell {
UITextField *textField;}@property (nonatomic, retain) UITextField *textField;@endTextFormCell.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];}它可能看起来有点冗长,但它确实有效!
别忘了设置代表!
- 3 回答
- 0 关注
- 738 浏览
添加回答
举报
0/150
提交
取消
