gpt4 book ai didi

ios - 在uitableview中识别许多单元格

转载 作者:行者123 更新时间:2023-11-29 01:11:45 25 4
gpt4 key购买 nike

我想在 TableView 中识别许多自定义单元格“我在 Storyboard中构建它们”,但错误要求我返回值,我试图返回 nil 和 int 值和单元格,但错误是相同的

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->  UITableViewCell {
if indexPath == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("CheefsCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 1 {

let cell = tableView.dequeueReusableCellWithIdentifier("BeautyCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 2 {
let cell = tableView.dequeueReusableCellWithIdentifier("StudentServicesCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 3 {
let cell = tableView.dequeueReusableCellWithIdentifier("ArtAndDesigneCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 4 {
let cell = tableView.dequeueReusableCellWithIdentifier("StoreCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 5 {
let cell = tableView.dequeueReusableCellWithIdentifier("OthersCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
return

}

更新:: git hub link /

最佳答案

方法cellForRowAtIndexPath需要返回一个非可选的UITableViewCell,因此您必须确保在任何情况下都返回一个单元格。不允许使用简单的 returnreturn nil

代码可以精简,大部分是多余的。唯一的区别是标识符。

一个合适的解决方案是在索引路径的 row 属性上使用 switch 语句。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->  UITableViewCell {

var identifier : String
switch indexPath.row {
case 0: identifier = "CheefsCell"
case 1: identifier = "BeautyCell"
case 2: identifier = "StudentServicesCell"
case 3: identifier = "ArtAndDesigneCell"
case 4: identifier = "StoreCell"
default: identifier = "OthersCell"
}
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
cell.accessoryType = .DisclosureIndicator
return cell
}

关于ios - 在uitableview中识别许多单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35663246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com