具有2个原型单元。每个都有不同的大小,所以我有cell和cell1。单元格应该为40,而单元格必须为75。
我尝试使用heightForRowAt-发现它在cellForRowAt之前被调用
我厌倦了在故事板上为每个单元格设置高度
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "starshipCell", for: indexPath)
let cell1 = tableView.dequeueReusableCell(withIdentifier: "starshipCell1", for: indexPath) as! SectionTableViewCell
switch indexPath {
case [4,0]:
cell1.sectionLbl.text = "Armor".lowercased()
cell1.detailTextLabel?.text = "Test"
return cell1
case [4,1]:
cell.textLabel?.text = "Defensive Countermeasures"
return cell
case [4,2]:
cell.textLabel?.text = "Shields"
return cell
case [11, 0]:
cell.textLabel?.text = "Forward Arc"
return cell
case [11, 1]:
cell.textLabel?.text = "Port Arc"
return cell
case [11, 2]:
cell.textLabel?.text = "Starboard Arc"
return cell
case [11, 3]:
cell.textLabel?.text = "Aft Arc"
return cell
case [11, 4]:
cell.textLabel?.text = "Turret"
return cell
default:
return cell
}
// return cell
}
'尝试使同一索引路径的多个单元出队,这是不允许的。如果确实需要使超出表视图要求的更多单元出队,请使用-dequeueReusableCellWithIdentifier:方法(无索引路径)。单元格标识符:starshipCell1,索引路径:{length = 2,path = 0-0}'
因此,上面的方法很完美。我只需要调整这些单元格的行高。现在代码还不是100%完成。以上所有情况将更改并更新为cell1。错误仅当我使用heightForRowAt
时答案 0 :(得分:0)
在cellForRowAt
方法中,您只能使一个单元出队并返回。为了在给定的索引路径下返回正确的索引,通常的做法类似于以下内容:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell
if indexPath.row == 0 { // starshipCell only appears if it's the first row
cell = tableView.dequeueReusableCell(withIdentifier: "starshipCell", for: indexPath)
} else { // Otherwise, we use starshipCell1
cell = tableView.dequeueReusableCell(withIdentifier: "starshipCell1", for: indexPath) as! SectionTableViewCell
}
// Set up the cell here
return cell
}
这样,每个单元仅对dequeueReusableCell调用一次。 (错误)heightForRowAt
应该类似使用。