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

How to set UITextField inside UITableViewCell (grouped)? I want the user to be able to edit it.

+5
source share
3 answers

Add UITextFieldas a subitem UITableViewCell contentView:

[mycell.contentView addSubview:view];
+5
source

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 contents TextFieldController.m

In addition, there is a lot of other great code for working with UIKit objects.

+2

This is how I implemented it in my application, but you 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 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 this helps.

+1
source

All Articles