我需要为下一个iOS应用创建一个包含多个列和行的表。有固定数量的列和行,每个单元格中只需要一个UITextField。我只是想知道使用两个for循环来创建它是否是一个好主意。 像这样:
let columns = 7
let rows = 12
var firstRow = true
for col in 1 ... columns {
for row in 1 ... rows {
let w = Double(contentWidth / columns)
let h = Double(contentHeight / rows)
let x = Double(col - 1) * w
let y = Double(row - 1) * h
let cell = UITextView(frame: CGRect(x: x, y: y, width: w, height: h))
cell.tag = ("\(col)\(row)" as NSString).integerValue
cell.backgroundColor = cellColour(cell.text)
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.font = UIFont(name: "AvenirNext-Regular", size: 15)
cell.textAlignment = .center
cell.layer.borderWidth = 1
cell.delegate = self
contentView.addSubview(cell)
}
}
以前有人曾经这样试过吗?