2

I do not know if this is possible or (more than likely) there is a better way of achieving my goal.

I have created a tableview using sections and items programatically:

let sections = ["One","Two","Three","Four"]

let items = [["Text here"],["Text here"],[BUTTON HERE],["Text Here"]]

I need a button within the tableview which loads an external website.

Layout similar to this:

Section ONE

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

BUTTON TO EXTERNAL SITE

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

It needs to be a button to support my UX Research.

I would be happy for the button to appear in the text rather than in a separate table view section if it is easier.

UPDATE: A suitable solution from @vadian is to use this:

var string = "Google"
var attributedString = NSMutableAttributedString(string: string, attributes:[NSLinkAttributeName: URL(string: "http://www.google.com")!])

yourTextView.attributedText = attributedString

How can I add this to the array?

Community
  • 1
  • 1
128K
  • 63
  • 1
  • 7
  • Thanks for looking, I really appreciate it – 128K Aug 28 '17 at 13:10
  • 1
    Do you really need a button for that? Isn't putting a clickable link inside a UITextView enough? If it is, have a look at this [question](https://stackoverflow.com/questions/21629784/how-to-make-a-clickable-link-in-an-nsattributedstring-for-a) – Dávid Pásztor Aug 28 '17 at 13:12
  • 1
    You cannot use a *button* (a **view**) in a data source array (the **model**) – vadian Aug 28 '17 at 13:13
  • Thanks for all of the advice. Is there a good way to achieve this? I have looked at the link above. I'm not sure how I can add this: `var string = "Google" var attributedString = NSMutableAttributedString(string: string, attributes:[NSLinkAttributeName: URL(string: "http://www.google.com")!]) yourTextView.attributedText = attributedString` – 128K Aug 28 '17 at 13:17

1 Answers1

1

Add a button to your cell. Make your array elements [string,bool], and in your tableview, show and hide the button based on the bool for each element of the array.

solenoid
  • 954
  • 1
  • 9
  • 20
  • This is a good option. Thank you. Is there an optimal way to achieve this? I've just been looking for examples. – 128K Aug 28 '17 at 13:18
  • 1
    Start with how to implement custom cells in a tableView - it's a bit outside the scope here and is handled in lots of examples better than I could explain. – solenoid Aug 28 '17 at 13:20
  • Thanks, I'll take a look – 128K Aug 28 '17 at 13:21