iosDev
iosDev

Reputation: 604

how to add textfield in tableview cell(each row) and set tag to each textfield to access it's text

How can i add textfield in tableview cell (on each row). This textfield will be in middle of each row. And also set Tag on each textfield of cell to access their text.

Upvotes: 3

Views: 5927

Answers (1)

WhiteTiger
WhiteTiger

Reputation: 1691

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];
}
...

Upvotes: 4

Related Questions