0
votes

I am working on a app using TableView now i am facing an issue listed below.

  • Inside my TableView there is UITextView on it, that MUST be selectable, but not editable (because I need to use and proceed links).

My issue is:
when I tap on a link as everybody does, it doesn't work. I need to hold it a bit longer to make it work. I thought that it is because of "Selectable" property brings in a Double Tap Gesture recognizer, so my textView checks if there is a second tap, but I don't know how to find and remove only double tap recognizer.

What should I do?

Thank you.

3
Why can't use long press gesture recognizer?user3182143
show your coding for helpinguser3182143
@user3182143 There is no much coding. UITextView is created in storyBoard. In cellForRowAtIndexPath I add some links using attributedText.addAttribute(NSLinkAttributeName, value: linkAddress, range: linkWordRange); And then just assign attributedText to textView.And I need my link to be opened easily by tapping.Nikolay Pryahin
Check my code.It works fine.user3182143

3 Answers

1
votes

Have you considered replacing the TextView with a UIWebView, and just do a loadHTMLString function?

This way when you tap on a link, it will open instantly? You can even have a UIWebView delegate and do what you want when the link is pressed(Custom UIWebView instead of auto opening in safari etc)

0
votes

You've to handle tap event.. Through this code tapGesture.numberOfTapsRequired = 1

OR

To do this, you will need to embed one in your UITableViewCell. But there's no need to create a custom cell. Here is the basic idea of what you will want to do:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UITextView *comment = [[UITextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, tableView.rowHeight)];
        comment.editable = NO;
        comment.delegate = self;
        [cell.contentView addSubview:comment];
        [comment release];
    }
    return cell;
}

You will, of course, need to set your rowHeight if you don't want the standard 44pt height that comes with the cell. And if you want actual cells, you'll need to add your own logic so that only the cell you want is a textView, but this is the basic idea. The rest is yours to customize to your fitting. Hope this helps

EDIT: to bypass the textView to get to your cell, there are two ways to go about this.

1) you can make a custom textView class and overwrite touchesBegan to send the message to super:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
}

this will send the touch events to its superview, which would be your tableView. Considering you didn't want to make custom UITableViewCells, I imagine you probably don't want to make a custom textView class either. Which leads me to option two.

2) when creating the textView, remove comment.editable = NO;. We need to keep it editable, but will fix that in a delegate method.

In your code, you will want to insert a textView delegate method and we'll do all our work from there:

EDIT: changing this code to use with a UITableViewController

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// this method is called every time you touch in the textView, provided it's editable;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:textView.superview.superview];
    // i know that looks a bit obscure, but calling superview the first time finds the contentView of your cell;
    //  calling it the second time returns the cell it's held in, which we can retrieve an index path from;

    // this is the edited part;
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    // this programmatically selects the cell you've called behind the textView;


    [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    // this selects the cell under the textView;
    return NO;  // specifies you don't want to edit the textView;
}

If that's not what you wanted, just let me know and we'll get you sorted out

0
votes

Finding and Removing Double Tap Gesture recognizer

Objective C

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
  if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) 
  {
     [(UITapGestureRecognizer *)gestureRecognizer setNumberOfTapsRequired:1];
     gestureRecognizer.enabled = NO;
  }
}

Swift

func addGestureRecognizer(gestureRecognizer: UIGestureRecognizer)
{
    if gestureRecognizer.isKindOfClass(UITapGestureRecognizer)
    {
       (gestureRecognizer as! UITapGestureRecognizer).numberOfTapsRequired = 1
       gestureRecognizer.enabled = false
    }

}