swift - Click on keyword in UILabel -
my following code in swift.
to sum up, have uitableview tweets, each row tweet. can know hashtags king of code :
for hashtag in tweet.hashtags { attributedtext.setattributes(hashtagattrs, range: hashtag.nsrange) }
but useful if want highlight these keywords (hashtags)
to put text of tweet in label, write :
@iboutlet weak var tweettextlabel: uilabel! tweettextlabel?.text = tweet.text
so have tableview
what want able click on hashtag , research same information new hashtag. ask : how can click on hashtag, directly in label ?
thank much!
[update]
i succeed access hashtag uitextview instead of uilabel, code :
func textview(textview: uitextview, shouldinteractwithurl url: nsurl, inrange characterrange: nsrange) -> bool { let tweettableview = tweettableviewcontroller() tweettableview.searchtext = url.absolutestring return true }
but problem can't reload data in tableview. have new tweets in tweet variable, cell doesn't update. function tableview 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 tried
tableview.delegate = self
i use function refresh tableview, self.tableview.reloaddata() 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 use instance of class here ? should call parent tableview of 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 succeed delegate : problem used instance of tableviewcontroller.
// tableviewcell protocol tweettableviewcelldelegate { // delegate func updatetweethashtag(tweettableviewcell) } // tableviewcontroller func updatetweethashtag(cell: tweettableviewcell){ let newhashtag = cell.newhashtagincell searchtext = newhashtag }
i don't think uilabels support clickable urls. you'll need use uitextviews that, , implement appropriate delegate methods.
Comments
Post a Comment