【发布时间】:2018-12-23 06:24:17
【问题描述】:
我们可以将UITableview 转换为UITableviewCell 吗?
据我所知,我很困惑,向下转换是超类和子类之间的过程,但在下面的编码中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell=tableView.dequeueReusableCell(withIdentifier: "Categorycell")as? CategoryCell{
let category=DataService.instance.getCategories()[indexPath.row]
cell.updateViews(categories: category)
return cell
}else{
return CategoryCell()
}
}
UITableview 已向下转换为 UItableviewCell
但我跳过那些类型定义两者都不相关(通过层次结构)
class CategoryCell: UITableViewCell {
@IBOutlet weak var categoryImage:UIImageView!
@IBOutlet weak var categoryTitle:UILabel!
func updateViews(categories:Category) {
categoryImage.image=UIImage(named: categories.imageName)
categoryTitle.text=categories.title
}
}
【问题讨论】:
-
"
Can we downcast to UItableview to UItableviewcell?",不。你想达到什么目的? -
既然你没有在你的代码示例中贬低他们,你说我看不到这个问题的重点。
标签: swift