英文:
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=" "
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
// while i<ChosenMoves.count{
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 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) -> UITableViewCell {
let Cell=Table.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论