开发者

UITable Cell not selectable where there is a UITextView in the cell

I've added a UITextView to my cell. If the userclicks on the cell where the UITextView then then didSelectRowAtIndexPath: is not called unless they click the area where the UITextView is not covering. Is there a work a开发者_开发百科round or what is it I have done wrong?

UITextView *txtview = [[UITextView alloc] initWithFrame:CGRectMake(75, 1, 250, 34)];//init and create the UIWebView
txtview.autoresizesSubviews = NO;
//txtview.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[txtview setBackgroundColor:[UIColor grayColor]];
[txtview setFont:[UIFont fontWithName:@"Helvetica" size:15]];
txtview.textColor = [UIColor blackColor];
txtview.scrollEnabled = NO;

[txtview setOpaque:NO];
//[txtview setDelegate:self];
txtview.editable = NO;
txtview.text = aBlogRss.title;
[txtview setContentInset:UIEdgeInsetsMake(-11,-8,0,0)];
//[txtview loadHTMLString:htmlString baseURL:nil];
[cell addSubview:txtview];
[txtview release]; 

Many Thanks, -Code


I think the solution is actually

txtView.userInteractionEnabled = NO;

This will pass touches to the parent view.. or in this case the UITableViewCell. If you are doing this in multiple places it would be a good idea to subclass UITAbleViewCell.

Alternatively, you could make your class a UITextViewDelegate, set your txtView.delegate to self, and implement

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView ,

and then select the proper UITableViewCell through self.tableview.. But that would be nothing short of a hack.


You should create your UITextView in viewDidLoad: and only keep [cell addSubview:txtview]; in your cellForRowAtIndexPath:.

Alternatively, you can use: [cell setAccessoryView: txtView];

Another thing, how are you creating your cell?


Is the cell used once or potentially unlimited number of times? If once, then you can use

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition

and manually insert the indexPath since you know where it lies.

If the cell is to be re-used, try setting the "tag" property of the textfield with the row, then when the touch is detected by the UITextView, use one of its delegate methods to call the above UITableView method, providing an NSIndexPath derived from the UITextView's tag.


  1. Add an UITapGestureRecognizer to the cells.

  2. Write a gesture handling code like below.

    - (IBAction)handleTap:(UITapGestureRecognizer *)sender
    {
        [self.tableView selectRowAtIndexPath:
         [self.tableView indexPathForCell:(UITableViewCell *)sender.view]
                                    animated:NO
                              scrollPosition:UITableViewScrollPositionNone];
    }
    
  3. Connect them.

You would obtain both selectable UITextView and selectable UITextViewCell.


Yes, it makes sense that the row does not get selected as there is a textView over it. Here are a couple of ways of going about this.

  1. You can make the textView frame such that you provide room for selecting the row.

  2. You can set each textView (for each row) as a property and find out which textView was selected. Based on that you can call the textViewDidBeginEditing method and programmatically make that row selectable.

    [self.tableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionNone];
    

    References: How do I select a UITableViewCell by default?

    Select tableview row programmatically


Note: You will have to logically link each textView with the it's corresponding cellIndexPath.


try

txtView.userInteractionEnabled = YES;

That should do it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜