如何在具有两个自定义UITableViewCells的一个视图控制器中创建两个表视图? [英] How do I create two table views in one view controller with two custom UITableViewCells?
问题描述
我试图使用两个自定义的 UITableViewCells
在一个视图控制器中创建两个 UITableViews
.我有以下内容:
I am trying to create two UITableViews
in one view controller using two custom UITableViewCells
. I have the following:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == self.tableView {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomOne") as! CustomOneTableViewCell
return cell
}
if tableView == self.autoSuggestTableView {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTwo") as! CustomTwoTableViewCell
return cell
}
}
但是我不断收到错误消息:
But I keep getting the error:
Missing return in a function expected to return 'UITableViewCell'
方法结束时我必须返回什么?
What do I have to return in the end of the method?
推荐答案
出现该错误是因为,如果由于某种原因,表视图不是您编写的两个选项中的一个,则它没有任何值可返回,只需在末尾添加一个默认值:
The error appears because if for any reason, the table view is non of the two options that you wrote, then it doesn't have any value to return, just add a default value at the end:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == firstTableView,
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomOne") as? CustomOneTableViewCell {
return cell
} else if tableView == autoSuggestTableView,
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTwo") as? CustomTwoTableViewCell {
return cell
}
return UITableViewCell()
}
已更新至Swift 4.1.2:我还将此答案更新为版本 4.1.2
,因为该方法的返回值
不能为 nil
,因此已修改为默认值,哑元 UITableViewCell
.
Updated to swift 4.1.2:
I've updated this answer to version 4.1.2
, also, because the return value
of the method cannot be nil
, modified to a default, dummy UITableViewCell
.
这篇关于如何在具有两个自定义UITableViewCells的一个视图控制器中创建两个表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!