Click on keyword in UILabel

299 views Asked by At

My following code is in Swift.

To sum up, I have a UITableView with tweets, each row a tweet. I can know where are the hashtags with this king of code :

for hashtag in tweet.hashtags {
    attributedText.setAttributes(hashtagAttrs, range: hashtag.nsrange)
}

But it is useful if I want to highlight these keywords (hashtags)

To put the text of the tweet in my Label, I write :

@IBOutlet weak var tweetTextLabel: UILabel!
tweetTextLabel?.text = tweet.text

So now I have this tableView Screenshot of my tableView

What I want is to be able to click on a hashtag and research the same information with this new hashtag. What I ask : How can I click on my hashtag, directly in my label ?

Thank you so much!

[update]

I succeed to access my hashtag with a UITextView instead of UILabel, with this code :

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {    
    let tweetTableView = TweetTableViewController()
    tweetTableView.searchText = URL.absoluteString
    return true
}

But the problem now is I can't reload my data in my tableView. I have my new tweets in my tweet variable, but my cell doesn't update. This function tableView is never called!

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath) as! TweetTableViewCell
    cell.tweet = tweets[indexPath.section][indexPath.row]
    return cell
}

I've already tried to

tableView.delegate = self

I use this function to refresh my tableView, but self.tableView.reloadData() do nothing ...

@IBAction func refresh(sender: UIRefreshControl?) {
    if searchText != nil {
        if let request = nextRequestToAttempt {
            request.fetchTweets { (newTweets) -> Void in
                dispatch_async(dispatch_get_main_queue()) { () -> Void in
                    if newTweets.count > 0 {
                        self.lastSuccessfulRequest = request
                        self.tweets.insert(newTweets, atIndex: 0)
                        self.tableView.reloadData()
                    }
                    sender?.endRefreshing()
                }
            }
        }
    } else {
        sender?.endRefreshing()
    }
}

HINT :

Maybe it's because I use another instance of my class here ? Should I call the parent tableView of my tableViewCell ? How ?

 func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {    
    let tweetTableView = TweetTableViewController()
    tweetTableView.searchText = URL.absoluteString
    return true
}

[UPDATE & CLOSE]

I finally succeed with a delegate : The problem was I used another instance of my tableViewController.

// TableViewCell
protocol TweetTableViewCellDelegate  { // my delegate
    func updateTweetHashTag(TweetTableViewCell)
}

// TableViewController 
func updateTweetHashTag(cell: TweetTableViewCell){
    let newHashTag = cell.newHashTagInCell
    searchText = newHashTag
}
1

There are 1 answers

0
Duncan C On

I don't think UILabels support clickable URLs. You'll need to use UITextViews for that, and implement the appropriate delegate methods.