在 swift return 'UITableViewCell' 的函数中出现 Missing return 之类的错误
Getting error like Missing return in a function expected to return 'UITableViewCell' in swift
正在尝试在表格视图中显示 3 个部分。我有一个部分的枚举。但是出现如下错误:-
- “numberOfRowsInSection”
中预期 return 'Int' 的函数中缺少 return
- 在“cellForRowAt indexPath”
中预期 return 'UITableViewCell' 的函数中缺少 return
enum TableSection: Int {
case ProfileNameAndImage = 0, FollowAndFollowings, Options
}
//MARK:- UITableViewDataSource
extension ProfileViewController: UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return TableSection.Options.rawValue
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let tableSection = TableSection(rawValue: section){
switch tableSection {
case .ProfileNameAndImage:
return 1
case .FollowAndFollowings:
return 1
case .Options:
return ProfileOptionViewModel.allCases.count
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let tableSection = TableSection(rawValue: indexPath.section){
switch tableSection {
case .ProfileNameAndImage:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileImageAndNameTableViewCell") as? ProfileImageAndNameTableViewCell else {
return UITableViewCell()
}
cell.userData = ProfileImageAndNameModel(userName: "John", userId: "12345", userImage: #imageLiteral(resourceName: "bird"))
return cell
case .FollowAndFollowings:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as? ProfileTableViewCell else {
return UITableViewCell()
}
return cell
case .Options:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as? ProfileTableViewCell else {
return UITableViewCell()
}
cell.profileData = ProfileOptionViewModel(rawValue: indexPath.row)
return cell
}
}
}
}
在这两种情况下,如果 if let
检查失败,您必须 return 一些东西。
首先,枚举案例应该以小写字母开头
enum TableSection: Int {
case profileNameAndImage = 0, followAndFollowings, options
}
In numberOfRows
return 0 如果检查失败——实际上永远不会发生
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let tableSection = TableSection(rawValue: section) else { return 0 }
switch tableSection {
case .profileNameAndImage, .followAndFollowings:
return 1
case .options:
return ProfileOptionViewModel.allCases.count
}
}
In cellForRow
强制解包该部分(如果 numberOfRows
为 0 则不调用该方法)并强制向下转换所有单元格。代码不能崩溃。如果是,则表明存在设计错误。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableSection = TableSection(rawValue: indexPath.section)!
switch tableSection {
case .profileNameAndImage:
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileImageAndNameTableViewCell") as! ProfileImageAndNameTableViewCell
cell.userData = ProfileImageAndNameModel(userName: "John", userId: "12345", userImage: #imageLiteral(resourceName: "bird"))
return cell
case .followAndFollowings:
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as! ProfileTableViewCell
return cell
case .options:
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as! ProfileTableViewCell
cell.profileData = ProfileOptionViewModel(rawValue: indexPath.row)
return cell
}
}
请注意,followAndFollowings
return 是一个没有数据的单元格。 TableSection.Options.rawValue
是 2,但是你有 3 个部分,所以可以硬编码 return 值
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
正在尝试在表格视图中显示 3 个部分。我有一个部分的枚举。但是出现如下错误:-
- “numberOfRowsInSection” 中预期 return 'Int' 的函数中缺少 return
- 在“cellForRowAt indexPath” 中预期 return 'UITableViewCell' 的函数中缺少 return
enum TableSection: Int {
case ProfileNameAndImage = 0, FollowAndFollowings, Options
}
//MARK:- UITableViewDataSource
extension ProfileViewController: UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return TableSection.Options.rawValue
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let tableSection = TableSection(rawValue: section){
switch tableSection {
case .ProfileNameAndImage:
return 1
case .FollowAndFollowings:
return 1
case .Options:
return ProfileOptionViewModel.allCases.count
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let tableSection = TableSection(rawValue: indexPath.section){
switch tableSection {
case .ProfileNameAndImage:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileImageAndNameTableViewCell") as? ProfileImageAndNameTableViewCell else {
return UITableViewCell()
}
cell.userData = ProfileImageAndNameModel(userName: "John", userId: "12345", userImage: #imageLiteral(resourceName: "bird"))
return cell
case .FollowAndFollowings:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as? ProfileTableViewCell else {
return UITableViewCell()
}
return cell
case .Options:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as? ProfileTableViewCell else {
return UITableViewCell()
}
cell.profileData = ProfileOptionViewModel(rawValue: indexPath.row)
return cell
}
}
}
}
在这两种情况下,如果 if let
检查失败,您必须 return 一些东西。
首先,枚举案例应该以小写字母开头
enum TableSection: Int {
case profileNameAndImage = 0, followAndFollowings, options
}
In numberOfRows
return 0 如果检查失败——实际上永远不会发生
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let tableSection = TableSection(rawValue: section) else { return 0 }
switch tableSection {
case .profileNameAndImage, .followAndFollowings:
return 1
case .options:
return ProfileOptionViewModel.allCases.count
}
}
In cellForRow
强制解包该部分(如果 numberOfRows
为 0 则不调用该方法)并强制向下转换所有单元格。代码不能崩溃。如果是,则表明存在设计错误。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableSection = TableSection(rawValue: indexPath.section)!
switch tableSection {
case .profileNameAndImage:
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileImageAndNameTableViewCell") as! ProfileImageAndNameTableViewCell
cell.userData = ProfileImageAndNameModel(userName: "John", userId: "12345", userImage: #imageLiteral(resourceName: "bird"))
return cell
case .followAndFollowings:
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as! ProfileTableViewCell
return cell
case .options:
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as! ProfileTableViewCell
cell.profileData = ProfileOptionViewModel(rawValue: indexPath.row)
return cell
}
}
请注意,followAndFollowings
return 是一个没有数据的单元格。 TableSection.Options.rawValue
是 2,但是你有 3 个部分,所以可以硬编码 return 值
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}