简体   繁体   中英

Iteratively creating UITextViews based on number of items in an array

Quite new to swift and coding in general so sorry if this is a basic question, but I want to be able to procedurally create UITextViews based on the number of items in an array of strings. For example:

var stringArray = [“first sentence string”, “second sentence string”] 
//create two UITextViews with text property of “first sentence string” and “second sentence string”

In this case it's not too hard to just manually create two UITextViews to put the strings in, but I want to be able to update my view with as many text views as I need for the stringArray which will have a varying amount of items in it.

My first idea was iterating the name of the variable that creates the UITextView like:

for i in stringArray {
   var textView(i) = UITextView()
   //textView properties inserted here
   view.addSubView(textView(i))
}

But this doesn't work because textView(i) is not a valid declaration of a variable.

There is easier Swifty ways to go about this, but if you're just looking to learn, you could do something like this:

for i in 0 ..< stringArray.count {
    let text = stringArray[i]
    // Set some fixed height for the textView so you can space it out by that height
    let textViewHeight: CGFloat = 50.0
    let textView = UITextView()
    view.addSubview(textView)
    textView.frame = CGRect(x: 0, y: CGFloat(i)*textViewHeight, width: view.frame.width, height: textViewHeight)
    textView.text = text
}

It sounds like your problem is from trying to name the property textView(i) . You can't pass variables into the name of a property. In this case you wouldn't even need to keep track of the iteration of the textView (ie textView1 , textView2 , etc.) because once that iteration of the loop is done, you won't have a reference to it anymore. If you wanted some reference to these, you could add an array of TextViews as an instance property like this:

var stringArray = ["first sentence string", "second sentence string"]

var textViews = [UITextView]()

for i in 0 ..< stringArray.count {
    let text = stringArray[i]
    // Set some fixed height for the textView so you can space it out by that height
    let textViewHeight: CGFloat = 50.0
    let textView = UITextView()
    view.addSubview(textView)
    textView.frame = CGRect(x: 0, y: CGFloat(i)*textViewHeight, width: view.frame.width, height: textViewHeight)
    textView.text = text
    // Append the textView to the array
    textViews.append(textView)
}

Now you have access to any and all textViews within the array. Say somewhere in your code you wanted to access the nth textView in your array of textViews and change it's text, you could accomplish this by saying textViews[n].text = "updated text" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM