首页 > 解决方案 > Swift , Identing Table Views 每行缩进更多

问题描述

它看起来像什么

我正在努力做到这一点,所以每个字符的每个名称都会随着每一行越来越多地缩进,所以从左边开始,下一行向右移动一点,然后下一行向右移动一点。

我根本想不通为什么我不能一个一个地缩进我的标签。任何帮助表示赞赏。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var Dwarves = ["Sleepy", "Sneezy", "Bashful", "Dopy", "Grumpy", "Doc", "Happy", "Sad"]
    var imagess = ["Sleepy", "Sneezy", "Bashful", "Dopy", "Grumpy", "Doc", "Happy", "Sad"]

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Dwarves.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomTableViewCell

        cell.CellView.layer.cornerRadius = cell.CellView.frame.height / 2
        cell.Label.text = Dwarves[indexPath.row]
        cell.CharcterImage.image = UIImage(named: 

imagess[indexPath.row])
        cell.CharcterImage.layer.cornerRadius = cell.CharcterImage.frame.height / 2

        return cell
    }

    func tableView(_ tableView: UITableView, indentationLevelForRowAt  indexPath: IndexPath.) -> Int {

    }
}

我尝试使用,indentationLevelforRowAt但它似乎不起作用。

 import UIKit


class CustomTableViewCell: UITableViewCell {

@IBOutlet weak var CellView: UIView!
@IBOutlet weak var CharcterImage: UIImageView!
@IBOutlet weak var Label:UILabel!
@IBOutlet weak var TableView:UITableView!





override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

标签: iosswiftuitableview

解决方案


一种原始的方法是在字符串前面加上空格。这里我使用两个空格的缩进级别。

cell.Label.text = "\(String(repeating: " " , count: indexPath.row*2))\(Dwarves[indexPath.row])")

推荐阅读