views:

1713

answers:

1

Hi guys I have a tableview cell with a custom textview in it, I am now left wondering how do I possibly access the text in the textbox after text has been edited/added.

I would normally know how this is done when I draw the textfield through IB, but my textviewcell is dynamically drawn.

(i.e. I would like to capture the data that is updated in detailLabel.text)

Here is the related code to adapt to your answer. Thanks again!

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];


    //Big Text Box
    UITextView *detailLabel = [[UITextView alloc] initWithFrame:CGRectMake(30, 80, 500, 150)];
    detailLabel.tag = 20;
    [cell.contentView addSubview:detailLabel];
    detailLabel.layer.borderWidth = 1;
    detailLabel.layer.borderColor = [[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.9] CGColor];
    detailLabel.layer.cornerRadius = 10;
    detailLabel.font = [UIFont fontWithName:@"Helvetica" size:17];
    [detailLabel release];

} 

UITextView * detailLabel = (UITextView *) [cell.contentView viewWithTag:20];

switch (indexPath.row) {

case 0:
    detailLabel.text = @"no";
    break;
default:
    detailLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    detailLabel.hidden = NO;    

}
+1  A: 

You'll want to listen for messages sent by the UITextView. So look to implement the UITextViewDelegate protocol and register the implementing class with the detailLabel using its delegate property.

When your delegate is notified of a change in the UITextView you'll have to identify the cell which currently owns the UITextView. In doing so you must remember that cells can be reused.

I would start by looking at the following method in the UITableView class:

- (NSArray *)visibleCells

We know that if the user has made a change to the UITextView's contents it must currently be on screen and therefore be present in the pre-mentioned array. To find it we use the pointer to the UITextView which changed (it's a parameter in the textViewDidChange protocol method). So simply iterate over the visibleCells array and retrieve the UITextView and compare it against the UITextView which changed.

- (void)textViewDidChange:(UITextView *)textView {
....
cellsLabel = (UITextView *) [cell.contentView viewWithTag:20];
if (cellsLabel == textView)
...

You'll now have a handle to a cell in the UITableView. To find the index use the following UITableView method:

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
mmccomb
Ah, i See ;)First was the <UITextViewDelegate> trick in the .h and then the new method available is -(void)textViewDidChangeSelection:(UITextView *)textView
norskben
OK, so now I can access the text in the text field, but how do I know which textbox is sending this information.Lets say I have 15 cells, each with a textbox with different data.Rgds.
norskben
Edited original answer to clarify.
mmccomb
Thanks matt, I have this and a couple other related problems all resolved! Tonight I will sleep well. Thank you so much.
norskben