How to add a text field to a tableview cell (each row) and set a tag for each text field to access it.

How to add a text box to a tableview cell (in each row). This text box will be in the middle of each line. And also set a Tag on each text box of the cell to access their text.

+3
source share
1 answer

Of course you can, a small example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"test"];
    if(cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"test"] autorelease];

        UITextView *tv = [[UITextView alloc] initWithFrame:CGRectMake(0, (cell.contentView.bounds.size.height-30)/2, cell.contentView.bounds.size.width, 30)];
        [cell.contentView addSubview:tv];
        [tv setDelegate:self];
        tv.tag = indexPath.row;
    }

    return cell;
}

...
- (void)textViewDidEndEditing:(UITextView *)textView {
    NSLog(@"%d", textView.tag);

    [textView resignFirstResponder];
}
...
+5
source

All Articles