Search code examples
objective-cxcodeuitableviewxcode4.5

Xcode create textfield in UITableViewController


I'm trying to make a tableview in which a user can enter data into it. I want it to look almost identical to the contact app on the iPhone when you are creating a new contact.

I posted my code below.

Next to the title text, I would like the user to enter in a title, next to description text, I would like the user to enter in a description, next to the time, I would like the user to enter in a time, and next to the location text I would like the user to enter in a location.

I am completely lost on how to do this and any help would be greatly appreciated!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    if(indexPath.row == 0) {
        cell.textLabel.text = @"Title";
    }
    else if(indexPath.row == 1) {
        cell.textLabel.text = @"Description";
    }
    else if(indexPath.row == 2) {
            cell.textLabel.text = @"Time:";
    }
    else if(indexPath.row == 3) {
            cell.textLabel.text = @"Location:";
    }

    // Configure the cell...

    return cell;
}

Solution

  • You may create a custom cell with the UILabel and UITextfield then reuse the custom cell

    Using the custom cell in cellForRowAtIndexPath: as follows

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        LoginCell *cell = (LoginCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"LoginCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
    
        if (indexPath.row == 0) {
            cell.txtBox.placeholder = @"[email protected]";
            cell.lbllabel.text = @"Email";
        }
        else {
            cell.txtBox.placeholder = @"Required";
            cell.lbllabel.text = @"Password";
        }
        cell.Tag = indexPath.row;
    
        return cell;
    }
    

    Retrieving value from the textbox

    NSIndexPath *indexEmail = [NSIndexPath indexPathForRow:0 inSection:0];
    LoginCell *cellEmail = (LoginCell *)[loginTableView cellForRowAtIndexPath:indexEmail];
    NSString *strEmail = cellEmail.txtBox.text;
    
    NSIndexPath *indexPassword = [NSIndexPath indexPathForRow:1 inSection:0];
    LoginCell *cellPassword = (LoginCell *)[loginTableView cellForRowAtIndexPath:indexPassword];
    NSString *strPassword = cellPassword.txtBox.text;
    

    Sample Project