将tableview单元格文本标签附加到一个空数组中

问题描述

我想获取显示在表格视图单元格上的所有索引路径整数。现在,单元格上的数字是1-5。那就是应该附加到数组emptyA上的内容。我试图在索引路径上进行压缩,但这不起作用。我的所有代码均在没有使用演示图板的情况下突出显示代码应该在加载后的视图中追加数组。

   import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var emptyA = [Int]()
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
        cell.textLabel?.text =    "\([indexPath.row+1])"
     
        return cell
    }
    
    var theTable = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.addSubview(theTable)
        theTable.frame = CGRect(x: 100,y: 100,width: 100,height: 300)
        theTable.delegate = self
        theTable.dataSource = self
        theTable.register(UITableViewCell.self,forCellReuseIdentifier: "cell")
        //need this part
        emptyA.append()
        
    }


}

解决方法

您可以通过from random import randrange from time import sleep from os import system while True: system("cls") rand = randrange(10) rand+=60 print("Random number: ",rand) sleep(1) 方法在indexPath.row中添加emptyA,即

tableView(_:cellForRowAt:)