在 tableview 中显示三个 reusbale 单元格 - Swift 5
Display three reusbale cells in tableview - Swift 5
我有 3 个带有 XIB 文件的 UITableViewCell,但是在我的 tableview 上我无法显示我的三个 UITableViewCell,只能加载前两个
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.item % 2 == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "CaptureNameTableViewCell", for: indexPath) as! CaptureNameTableViewCell
cell.nameTextField.text = ""
cell.nameTextField.tag = indexPath.row
cell.nameTextField.delegate = self
return cell
} else if indexPath.section == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "CapturePhotoTableViewCell", for: indexPath) as! CapturePhotoTableViewCell
cell.displayImage = imageView
cell.cellDelegate = self
cell.selectImage.tag = indexPath.row
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "PieChartTableViewCell", for: indexPath) as! PieChartTableViewCell
return cell
}
}
你需要在tableViewController:
中注册xib tableViewCells
首先在单元格 xib class 中创建一个 id
属性,用作单元格的重用标识符(使其名称与您的重用标识符相同您在 xib 文件中设置的单元格):
let id = "cellIdentifier" // Name this appropriately
然后在单元格的 xib 中创建一个 nib
方法 class:
func nib() -> UINib {
return UINib(named: "cellIdentifier", bundle: nil)
}
然后在生命周期方法内部,例如 viewDidLoad()
:
tableView.register(nib: cell.nib, forCellReuseIdentifier: cell.id)
最后,指定要在 table 视图中的哪个单元格 indexPath.row。 (table 视图中的第几行):
switch indexPath.row {
case 0: // First cell
let cell = tableView.dequeueReusableCell(withIdentifier: "CaptureNameTableViewCell", for: indexPath) as! CaptureNameTableViewCell
// Configure cell as before
return cell
case 1: // Second cell
let cell = tableView.dequeueReusableCell(withIdentifier: "CapturePhotoTableViewCell", for: indexPath) as! CapturePhotoTableViewCell
// Configure cell as before
return cell
case 2: // Third cell
let cell = tableView.dequeueReusableCell(withIdentifier: "PieChartTableViewCell", for: indexPath) as! PieChartTableViewCell
// Configure cell as before
return cell
default:
return UITableViewCell() // This should not be reached but a switch statement must cover all possibilities.
}
我有 3 个带有 XIB 文件的 UITableViewCell,但是在我的 tableview 上我无法显示我的三个 UITableViewCell,只能加载前两个
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.item % 2 == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "CaptureNameTableViewCell", for: indexPath) as! CaptureNameTableViewCell
cell.nameTextField.text = ""
cell.nameTextField.tag = indexPath.row
cell.nameTextField.delegate = self
return cell
} else if indexPath.section == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "CapturePhotoTableViewCell", for: indexPath) as! CapturePhotoTableViewCell
cell.displayImage = imageView
cell.cellDelegate = self
cell.selectImage.tag = indexPath.row
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "PieChartTableViewCell", for: indexPath) as! PieChartTableViewCell
return cell
}
}
你需要在tableViewController:
中注册xib tableViewCells首先在单元格 xib class 中创建一个 id
属性,用作单元格的重用标识符(使其名称与您的重用标识符相同您在 xib 文件中设置的单元格):
let id = "cellIdentifier" // Name this appropriately
然后在单元格的 xib 中创建一个 nib
方法 class:
func nib() -> UINib {
return UINib(named: "cellIdentifier", bundle: nil)
}
然后在生命周期方法内部,例如 viewDidLoad()
:
tableView.register(nib: cell.nib, forCellReuseIdentifier: cell.id)
最后,指定要在 table 视图中的哪个单元格 indexPath.row。 (table 视图中的第几行):
switch indexPath.row {
case 0: // First cell
let cell = tableView.dequeueReusableCell(withIdentifier: "CaptureNameTableViewCell", for: indexPath) as! CaptureNameTableViewCell
// Configure cell as before
return cell
case 1: // Second cell
let cell = tableView.dequeueReusableCell(withIdentifier: "CapturePhotoTableViewCell", for: indexPath) as! CapturePhotoTableViewCell
// Configure cell as before
return cell
case 2: // Third cell
let cell = tableView.dequeueReusableCell(withIdentifier: "PieChartTableViewCell", for: indexPath) as! PieChartTableViewCell
// Configure cell as before
return cell
default:
return UITableViewCell() // This should not be reached but a switch statement must cover all possibilities.
}