3 回答

TA貢獻1806條經驗 獲得超8個贊
在方法開始時聲明單元格,
根據部分和行號為單元格分配一個值,
fatalError()在所有“不應該發生”的情況下拋出
返回單元格。
另請注意,這些break語句不是必需的。Swift中的默認行為是不會陷入下一種情況。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: SettingsCell
switch(indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
cell = tableView.dequeueReusableCellWithIdentifier("cell0", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
default:
fatalError("Unexpected row \(indexPath.row) in section \(indexPath.section)")
}
case 1:
switch (indexPath.row) {
case 0:
cell = tableView.dequeueReusableCellWithIdentifier("cell10", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("cell11", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
default:
fatalError("Unexpected row \(indexPath.row) in section \(indexPath.section)")
}
default:
fatalError("Unexpected section \(indexPath.section)")
}
return cell
}
該fatalError()誤差函數被標記為@noreturn,所以編譯器知道程序的執行將不會從默認的情況下繼續。(這也有助于查找程序中的邏輯錯誤。)
在所有其他情況下,編譯器將驗證是否已分配值cell。
在Swift 1.2中,以這種方式初始化常量(let cell ...)的可能性是新的。
另外,您可以創建一個單元格并在每種情況下“立即”返回:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch(indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
let cell = tableView.dequeueReusableCellWithIdentifier("cell0", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
return cell
case 1:
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
return cell
default:
fatalError("Unexpected row \(indexPath.row) in section \(indexPath.section)")
}
case 1:
switch (indexPath.row) {
case 0:
let cell = tableView.dequeueReusableCellWithIdentifier("cell10", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
return cell
case 1:
let cell = tableView.dequeueReusableCellWithIdentifier("cell11", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
return cell
default:
fatalError("Unexpected row \(indexPath.row) in section \(indexPath.section)")
}
default:
fatalError("Unexpected section \(indexPath.section)")
}
}
再次,調用fatalError()解決了“缺少期望的返回”編譯器錯誤。
如果在每種情況下創建了不同種類的單元(具有不同的類),則此模式很有用。

TA貢獻1780條經驗 獲得超5個贊
方法中缺少return語句。
返回語句示例。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0{
switch (indexPath.row) {
case 0:
let cell: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell0", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
break
case 1:
let cell: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
break
default:
let cellId: NSString = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell
break
}
}
if indexPath.section == 1{
switch (indexPath.row) {
case 0:
let cell: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell10", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.redColor()
break
case 1:
let cell: SettingsCell! = tableView.dequeueReusableCellWithIdentifier("cell11", forIndexPath: indexPath) as! SettingsCell
cell.backgroundColor = UIColor.whiteColor()
break
default:
let cellId: NSString = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell
break
}
}
return cell
}
- 3 回答
- 0 關注
- 793 瀏覽
添加回答
舉報