ios - 在 UITableViewCell 中有一个 UITextField

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

要么我的表单正常工作,但文本不可见(尽管我将其颜色设置为蓝色),当我点击它时键盘会进入该字段并且我无法正确实现键盘事件。 我尝试了一堆来自 Apple 的示例(主要是 UICatalog,其中有一个类似的控件),但它仍然无法正常工作。

有人可以帮助我(以及所有试图实现此控件的人)并在 UITableViewCell 中发布一个 UITextField 的简单实现,效果很好吗?

最佳答案

试试这个。对我来说就像一个魅力(在 iPhone 设备上)。我曾经将此代码用于登录屏幕。我将表格 View 配置为有两个部分。你当然可以去掉部分条件。

- (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;    
}

结果如下所示:

https://stackoverflow.com/questions/409259/

相关文章:

objective-c - Objective-C Cocoa 应用程序中的正则表达式

objective-c - 将 UIImage 转换为 NSData

objective-c - 最佳实践? - 数组/字典作为核心数据实体属性

ios - 如何在 Swift 中设置返回按钮文本

objective-c - Objective-C中方法旁边的加号和减号是什么意思?

ios - 为 iOS 7 半透明 UINavigationBar 实现明亮、生动的色彩

ios - 如何在 NSAttributedString 中创建可点击链接?

ios - 以编程方式创建 UICollectionView

objective-c - 解决 Objective-C 命名空间冲突的最佳方法是什么?

ios - 有没有办法检查iOS应用程序是否在后台?