开发者

iphone tableview cells with custom textview - get textview reference

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 {

 开发者_StackOverflow中文版   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];


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


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.


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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜