views:

626

answers:

5

Hi guys I have a UITableView with 15 cells, each with a separate text box in it. I have implemented UITextViewDelegate and I am able to received changed textview data using textViewDidChange (etc). But I have one big problem still, how do I know WHICH textview sent this, (i.e. in which cell was the textview altered?)

Its interesting to have so much working, yet not know precisely where it comes from.

A whole bunch of code is available if required.

Regards @norskben

Code

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    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, CONST_Cell_width, 150)];
        detailLabel.tag = 20;
        [cell.contentView addSubview:detailLabel];

    }   


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

You can assign tags (integers) to the different views and query the tag number to see which view called the method. Look for the tag property on the view:

tag

The receiver’s tag, an integer that you can use to identify view objects in your application.

@property(nonatomic) NSInteger tag

see here

ennuikiller
+2  A: 

Not at my development machine, but when you create the UITextView you should be able to assign it a tag. I think it is [myTextView setTag:x]; where x is an integer.

Then, in the TextViewDidChange use if (textview.tag == x) { //do something } else if (textview.tag == y) { //do something else and so on }

Hope that helps a little.

Alan Taylor
A: 

Here's an important question: Are your text boxes static, or can they change over time? If they won't change (the user can't alter the number of cells or add more later), then you can declare a new textField for each cell. I have something similar in my apps. I have two text boxes, and depending on which textField is currently active, the delegate does something different.

Declare separate text fields in your header

UITextField *textField1;
UITextField *textField2;
UITextField *textField3;

in the delegate method, use if statement blocks to find out which textField is changing:

if (textField == textField1) {
    //do something
} else if (textField == myTextField2) {
    //something else
}

Note that this really only works if your view is static.

Hope this helps

Have a great day

JustinXXVII
What if a cell is reused? The original post mentions that there are 15 cells in the UITableView. Assuming that the original poster hasn't disabled cell reuse then there's no guarantee that a specific UITextView is always associated with the same cell.
mmccomb
You're right. You'd have to use a switch or if statement block to add each textField to the cell's contentView, explicitly choosing which textField goes in which cell indexPath.It's also interesting to note that when you scroll a textField off the screen, the data inside the textBox disappears as the cell is reloaded.
JustinXXVII
+1  A: 

The text views pass a reference to themselves in every delegate method so you know which one sent it. To make a connection to the cell, I'd set each text view's tag property to a different value that corresponds to the row of the cell they're in.

Ole Begemann
A: 

When you're searching the UITableView's cells for the event source UITextView, only iterate over the cells that the user can currently see. This can be obtained using the following UITableView method:

- (NSArray *)visibleCells
mmccomb