即使在代码编译后,也不会显示文本标签



class ViewController: UITableViewController {
var pictures = [String]()
override func viewDidLoad() {
super.viewDidLoad()
let fm = FileManager.default
let path = Bundle.main.resourcePath!
let items = try! fm.contentsOfDirectory(atPath: path)
for item in items{
if item.hasSuffix("@3x"){
pictures.append(item)
}
}
print(pictures)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictures.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath)
cell.textLabel?.text = pictures[indexPath.row]
return cell
}
}

我正在尝试为该行分配文件管理器/文件夹中的文件的文本标签。我的代码运行正常,但后缀为"@3x"的文件名的文本标签不可见。请帮忙。

获取完成后,您需要在tableView上调用reloadData方法。这是代码:

var pictures = [String]()
override func viewDidLoad() {
super.viewDidLoad()
let fm = FileManager.default
let path = Bundle.main.resourcePath!
let items = try! fm.contentsOfDirectory(atPath: path)
for item in items{
if item.hasSuffix("@3x"){
pictures.append(item)
}
}
print(pictures)
tableView.reloadData()
}

最新更新