Ios Swift TableViewCell阵列
我的情况是:我想将索引X处的数组中的数据保存在第1节索引X上的一行中 我的代码是:Ios Swift TableViewCell阵列,ios,arrays,swift,uitableview,tableview,Ios,Arrays,Swift,Uitableview,Tableview,我的情况是:我想将索引X处的数组中的数据保存在第1节索引X上的一行中 我的代码是: override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return setObje
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return setObjectToPass.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellEmpty = tableView.dequeueReusableCellWithIdentifier("LabelCell")
var countCell = 0
while countCell < setObjectToPass.count {
let indexPaths = NSIndexPath(forRow: countCell, inSection: 0)
let cell = tableView.dequeueReusableCellWithIdentifier( "LabelCell", forIndexPath: indexPaths)
cell.textLabel!.text = String(setObjectToPass[countCell])
print(cell)
countCell+=1
return cell
}
不要在
cellforrowatinexpath
delegate方法中使用循环cellForRowAtIndexPath
方法基于NumberOfRowInSection
计数行调用每行,只需使用indexpath.row
,使用此代码
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellEmpty = tableView.dequeueReusableCellWithIdentifier("LabelCell")
let cell = tableView.dequeueReusableCellWithIdentifier( "LabelCell", forIndexPath: indexPaths)
cell.textLabel!.text = setObjectToPass[indexPath.row] as? String
return cell
}
希望它对您有帮助您错误地实现了
tableView(:cellforrowatinexpath:indexath:)
方法
请记住,UITableViewDelegate
中的每个委托方法都像是在问你一个问题。例如,numberOfSectionsTableView(:)
就像是问你“你想在你的表视图中有多少节?”。您可以通过返回一个值来回答问题
tableView(u3;:cellforrowatinexpath:indexath:)
与此类似。它还提出了一个问题。它询问“我应该在这个索引路径的表行中显示什么?”
在您的代码中,似乎希望给出多个答案—在数组中循环并尝试多次返回。但这样不行,你只能给出一个答案
在while
循环的第一次迭代中,执行点击return
并停止。这就是为什么您只看到第一个表格单元格
因此,您应该更改代码,使其仅给出问题的一个答案:
let cell = tableView.dequeueReusableCellWithIdentifier("LabalCell")
cell.textLabel?.text = String(setObjectsToPass[indexPath.row])
return cell
我建议研究
UITableViewDataSource
的基本实现。你在cellforrowatinedexpath(:)
方法中所做的事情非常奇怪。好的,谢谢,我来试一试,还有什么建议吗?setObjectToPass数组值是什么?这些是整数值我正在为setObjectToPass“使用未解析标识符”。声明如下:class TableViewControllerResult:UITableViewController{//Fetch variable from other ViewController var counterToPass:Int!var counterToPass:Int!var setObjectToPass:[Int]!override func viewDidLoad(){super.viewDidLoad()print(counterToPass)}
Oops!我实际上指的是setObjectToPass
@HendrikBreezyIn与它的另一条注释tyvm!我使用String(setObjectToPass[indexPath.row])
保存标签文本,因为它现在无法将Int转换为String!
let cell = tableView.dequeueReusableCellWithIdentifier("LabalCell")
cell.textLabel?.text = String(setObjectsToPass[indexPath.row])
return cell