【发布时间】:2016-10-08 12:36:33
【问题描述】:
我是新手。
但我不明白为什么 UITableView 没有加载所有元素(数组中的 10 个元素)。
使用 Xcode7 有效,但使用 Xcode8 (Swift 3) 没有。
我重写了代码但无济于事:正在加载一个元素...
这是 UIViewController 内部的代码(扩展名为 UITableViewDelegate、UITableViewDataSource)
代码:
// MARK: UITableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return commentArray.count
}
// cell height
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = mytableView.dequeueReusableCell(withIdentifier: "CellComment", for: indexPath ) as! commentCell
// connect objects with our information from arrays
cell.usernameBtn.setTitle(usernameArray[indexPath.row], for: UIControlState.normal)
cell.usernameBtn.sizeToFit()
cell.commentLbl.text = commentArray[indexPath.row]
return cell
}
我认为问题出在cellForRowAt....
我也有以下错误:
BFTask在延续块中捕获到异常。不鼓励这种行为,并将在未来的版本中删除。捕获的异常:与 UITableView 一起使用的索引路径无效。传递给表视图的索引路径必须恰好包含两个指定节和行的索引。如果可能,请使用 UITableView.h 中 NSIndexPath 上的类别。
我不明白。 有人有想法吗? 谢谢
【问题讨论】:
-
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { } 用这个方法替换你的这个方法并检查它
-
@HimanshuMoradiya 不,OP 有正确的 Swift 3 方法名称。
-
是否添加了tableView.estimatedRowHeight = 40?你设置数据源了吗? tableView.dataSource = self?
标签: ios uitableview swift3