Answer the question
In order to leave comments, you need to log in
How to create multiple UIView through a loop?
Good evening. I'm trying to understand the development of interfaces for Ios.
There is a task: through the code, using a loop, generate four UIImageView and labels on the screen.
It should look like goods in an online store and the price to it from below.
I added all these elements to a separate UIView using the addSubview method.
Further, I don’t really understand how to change the data and the arrangement of elements when working in a loop in order to create these remaining 3 elements next to the main one.
import UIKit
class SecondViewController: UIViewController {
var allImages: [UIImage] = [UIImage(named: "Img1")!,
UIImage(named: "Img2")!,
UIImage(named: "Img3")!,
UIImage(named: "Img4")!]
var i = 0
override func viewDidLoad() {
super.viewDidLoad()
let myView = UIView(frame: CGRect(x: 30, y: 40, width: 140, height: 240))
myView.backgroundColor = .gray
view.addSubview(myView)
let images = UIImageView(frame: CGRect(x: 10, y: 10, width: 120, height: 180))
images.image = allImages[i]
myView.addSubview(images)
let myLabel = UILabel(frame: CGRect(x: 10, y: 200, width: 120, height: 25))
myLabel.backgroundColor = .blue
myView.addSubview(myLabel)
}
}
Answer the question
In order to leave comments, you need to log in
Since you wrote:
I will assume that your knowledge is initial , so my answer will be in this vein. If I'm wrong and you want to solve the problem in your own way - write, I'll tell you (although I will dissuade).
Let's call one item a cell . If a cell should occupy the entire screen from edge to edge in width - use UITableView (called a table), if there can be several cells in width - use UICollectionView (called a collection).
Cell Reuseimplemented both in the table and in the collection. Not all cells are stored in memory; they are initialized as needed. The principle of operation for both collections is similar: specify the number of elements and pass a view for each position. The position is called an index .
Method for the number of elements in the table:
As you can see, it returns an integer.
Method for returning a view, for a table, return a UITableViewCell object :
You can't just return a UIView , it doesn't implement a reuse protocol. But you can put any views you want in UITableViewCell. It's a kind of container. Before using a cell class, you need to register the class.
On my channel there is a video about reuse in the table. On the Apple website, you can read about the table and the collection .
This is the main concept. You have to learn what DataSource, Delegate, dimensions, Layout (for a collection) are. Don't worry, there are many tutorials written about this, even in Russian.
PS The author wanted to solve the problem using a loop and code, I give an example:
Let's take out the value of Y for the cycle. You can also declare height and width parameters here:
var currentY: CGFloat = 0
let width: CGFloat = 375
let height: CGFloat = 100
for i in 0...3 {
}
for i in 0...3 {
var view = UIView()
view.frame = CGRect.init(x: 0, y: currentY, width: width, height: height)
currentY += view.frame.height + 10
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question