xcode 如何将 UITextField 放在 UITableViewCell(分组)中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2487413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:13:12  来源:igfitidea点击:

How to put a UITextField inside of a UITableViewCell (grouped)?

iphonexcodeuitableviewuitextfield

提问by RexOnRoids

How to put a UITextField inside of a UITableViewCell (grouped)? I want a user to be able to edit it.

如何将 UITextField 放在 UITableViewCell(分组)中?我希望用户能够编辑它。

采纳答案by diederikh

Add the UITextFieldas an subview of the UITableViewCell's contentView:

添加UITextField作为 UITableViewCell 的子视图contentView

[mycell.contentView addSubview:view];

回答by Eric Schweichler

Apple's own UICatalog demo application has an example of placing UITextFields in Grouped UITableView Cells: http://developer.apple.com/iphone/library/samplecode/UICatalog/index.html

Apple 自己的 UICatalog 演示应用程序有一个将 UITextFields 放在 Grouped UITableView Cells 中的示例:http: //developer.apple.com/iphone/library/samplecode/UICatalog/index.html

Check the contents of TextFieldController.m

检查内容 TextFieldController.m

Plus there's lots of other great code there for working with UIKit Objects.

另外还有很多其他很棒的代码可以用于处理 UIKit 对象。

回答by Sebastien Peek

This is how I've implemented it into my application, however you'll obviously need to change a few things. Hope this helps.

这就是我在我的应用程序中实现它的方式,但是您显然需要更改一些内容。希望这可以帮助。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.

//adding all the UITextField's to the UITableViewCell is a pain in the ass. Pretty sure this is correct though.

if ([indexPath section] == 0) {
    tUser = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
    tUser.adjustsFontSizeToFitWidth = YES;
    tUser.textColor = [UIColor blackColor];

    tPass = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
    tPass.adjustsFontSizeToFitWidth = YES;
    tPass.textColor = [UIColor blackColor];

    if ([indexPath section] == 0) {
        if ([indexPath row] == 0) {
            tUser.placeholder = @"@JohnAppleseed";
            tUser.keyboardType = UIKeyboardTypeEmailAddress;
            tUser.returnKeyType = UIReturnKeyNext;
        }
        if ([indexPath row] == 1) {
            tPass.placeholder = @"Required";
            tPass.keyboardType = UIKeyboardTypeDefault;
            tPass.returnKeyType = UIReturnKeyDone;
            tPass.secureTextEntry = YES;
        }
    }

    tUser.backgroundColor = [UIColor whiteColor];
    tUser.autocorrectionType = UITextAutocorrectionTypeNo;
    tUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
    tUser.textAlignment = UITextAlignmentLeft;

    tPass.backgroundColor = [UIColor whiteColor];
    tPass.autocorrectionType = UITextAutocorrectionTypeNo;
    tPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
    tPass.textAlignment = UITextAlignmentLeft;

    tUser.clearButtonMode = UITextFieldViewModeNever;
    tPass.clearButtonMode = UITextFieldViewModeNever;

    [tUser setEnabled:YES];
    [tPass setEnabled:YES];

    //[tUser release];
    //[tPass release];
}
if ([indexPath section] == 0) { // Email & Password Section
    if ([indexPath row] == 0) { // Email
        cell.textLabel.text = @"Username";
        [cell addSubview:tUser];
        [tUser setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"twitter_name_preference"]];
    }
    else {
        cell.textLabel.text = @"Password";
        [cell addSubview:tPass];
        [tPass setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"twitter_pass_preference"]];
    }
}
return cell; }

Hope it helps.

希望能帮助到你。