Сначала установите высоту от UITableViewCell до UITableView.automaticDimension
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Вставьте все свои метки вопроса в UIStackView
(по вертикали), исключая bottomLabel
. Установите AutoLayoutConstraint между UIStackView
и bottomLabel
.
![enter image description here](https://i.stack.imgur.com/lhnhP.png)
Установите для свойства numberOfLines
UILabel
s значение 0 (ноль).
![enter image description here](https://i.stack.imgur.com/Mx3t5.png)
Установите Распределение UIStackView
как Fill
![enter image description here](https://i.stack.imgur.com/LUl1E.png)
Затем в вашем методе tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
скрыть метки. И он будет автоматически обрабатывать пробелы между UILabel
s
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.questionLabel1.text = labelOneText[indexPath.row]
cell.questionLabel2.text = labelTwoText[indexPath.row]
cell.questionLabel3.text = labelThreeText[indexPath.row]
if labelOneText[indexPath.row] == "" {
cell.questionLabel1.isHidden = true
}
if labelTwoText[indexPath.row] == "" {
cell.questionLabel2.isHidden = true
}
if labelThreeText[indexPath.row] == "" {
cell.questionLabel3.isHidden = true
}
return cell
}
Окончательный результат:
![enter image description here](https://i.stack.imgur.com/JyD09.png)