首先,我们创建一个简单的demo,在Main.storyboard中为ViewController添加一个导航控制器(Editor - Embed in - Navigation Controller),在ViewController中添加一个UITableView并添加约束,全屏显示,设置好相应的代理。在ViewController中实现部分代理方法,实现显示50个cell,页面效果如下:
代码如下:
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var dataSource = [String]()
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<50 {
dataSource.append("第\(i)个cell")
}
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
cell?.accessoryType = .disclosureIndicator
}
cell?.textLabel?.text = dataSource[indexPath.row]
cell?.detailTextLabel?.text = dataSource[indexPath.row]
return cell!
}
}
现在我想修改textLabel、detailTextLabel等显示内容的位置,右移10或者20