将数据加载到UITableViewCell单元格中

huangapple go评论64阅读模式
英文:

load data into UITableViewCell cell

问题

我完全是IOS开发的新手,我正在寻求关于我确信是一个简单问题的帮助。我正在构建一个测试应用程序来学习循环、代码、IBoutlets等,我已经创建了一个应用程序,可以将1-5个随机名称读入一个数组中,现在我想将这些元素读入同一屏幕上的一个表格中。如果我尝试使用WHILE循环,那么我会得到一个错误,说它找不到Return Cell,如果我不使用循环,那么它会在每个单元格中打印出所有元素。

谢谢

override func viewDidLoad() {
    super.viewDidLoad()
    // 在加载视图后执行任何额外的设置。
    Table.dataSource=self
}

@IBAction func Buttonpressed () {
    GetMoves()
}

func GetMoves() {
    var i=0
    var ArrayMove:String
    ChosenNumber=Int(Field.text!)
    ArrayMove=" "
    while i < ChosenNumber{
        ArrayMove=Moves[i]
        ChosenMoves.append(ArrayMove)
        i+=1
        self.Table.reloadData()
    }
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var movestring=" "
    var i=0
    
    let Cell=Table.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
    i=indexPath.row
    movestring=" "
    movestring=ChosenMoves[i]
    Cell.CellLabel.text=movestring
    i+=1

    return Cell
}

我希望每个数组元素出现在表格中的单独行上。

英文:

I'm totally new to IOS dev and I'm looking for help on what I'm sure is a simple issue. I'm building a test app to learn loops, code, IBoutlets etc and I have create an app that reads in between 1-5 random names into an array, I now want to read those elements into a table that I have on the same screen. If I try and use a WHILE loop then I get an error that it cannot find Return Cell, if I don't use a loop then it prints all elements in every cell.

thanks

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    Table.dataSource=self
}
@IBAction func Buttonpressed (){
    GetMoves()
}

func GetMoves() {
    var i=0
    var ArrayMove:String
    ChosenNumber=Int(Field.text!)
    ArrayMove=&quot; &quot;
    while i &lt; ChosenNumber{
        ArrayMove=Moves[i]
        ChosenMoves.append(ArrayMove)
        i+=1
        self.Table.reloadData()
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    return ChosenMoves.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    var movestring=&quot; &quot;
    var i=0
  //  while i&lt;ChosenMoves.count{
        let Cell=Table.dequeueReusableCell(withIdentifier: &quot;Cell&quot;, for: indexPath) as! CustomTableViewCell
        i=indexPath.row
        movestring=&quot; &quot;
        movestring=ChosenMoves[i]
        Cell.CellLabel.text=movestring
        i+=1
    
        return Cell
    
    }

I want each array element appears on an individual row in the table.

答案1

得分: 1

I think you are doing mistake while dequeuing cell. You have used Table where as you need to use tableView. I think there is no need of declaring variable i and similarly no need to increment it. Here is the updated method .

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
    Cell.CellLabel.text = ChosenMoves[indexPath.row]
    return Cell
}

I removed extra variables. One more point, above method by default gets called on every count. You don't need to use loop for it. Furthermore, I would suggest to go through swift language guide or 100 days of swift tutorial for better understanding.

英文:

I think you are doing mistake while dequeuing cell. You have used Table where as you need to use tableView. I think there is no need of declaring variable i and similarly no need to increment it. Here is the updated method .

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    let Cell=Table.dequeueReusableCell(withIdentifier: &quot;Cell&quot;, for: indexPath) as! CustomTableViewCell
    Cell.CellLabel.text = ChosenMoves[indexPath.row]
    return Cell
}

I removed extra variables. One more point, above method by default gets called on every count. You don't need to use loop for it. Furthermore, I would suggest to go through swift language guide or 100 days of swift tutorial for better understanding.

huangapple
  • 本文由 发表于 2023年7月13日 16:52:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定