1

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;
}
Thomas
  • 2,136
  • 4
  • 20
  • 57

2 Answers2

2

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 = @"example@gmail.com";
        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

icodebuster
  • 8,780
  • 7
  • 59
  • 63
0

Create an instance of the UITextView object with the frame that you need and then add it to the cell's contentView subview in the following manner

[cell.contentView addSubview:textView];

Hope this helps

Nanz
  • 75
  • 1
  • 9