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

后端 未结 3 863
你的背包
你的背包 2020-12-18 08:23

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

相关标签:
3条回答
  • 2020-12-18 08:30

    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

    Check the contents of TextFieldController.m

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

    0 讨论(0)
  • 2020-12-18 08:34

    Add the UITextField as an subview of the UITableViewCell's contentView:

    [mycell.contentView addSubview:view];
    
    0 讨论(0)
  • 2020-12-18 08:45

    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.

    0 讨论(0)
提交回复
热议问题