ios - 如何为 UITableView 中的单元格添加复选标记(使用自定义 UITableViewCell 子类和 dequeueReusableCell)
问题描述
我想在我的表格视图单元格中添加复选标记。我想只cell.accessoryType = UITableViewCellAccessoryCheckmark
在我的didSelectRowAt
方法中实现,但是在使用自定义子类时我无法让它工作UITableViewCell
。
自定义单元格:
class TableCell: UITableViewCell {
@IBOutlet weak var tableLabel: UILabel!
var arrayOneToDisplay:ArrayOne?
let arrayOne = ArrayOne()
func displayTable(_ currentArrayOne:ArrayOne) {
// Keep a reference to the cell
arrayOneToDisplay = currentArrayOne
// Get the Table Cells data
var tableCell = arrayOne.tableCells
// Set the instructionLabel
tableLabel.text = currentArrayOne.tableCells![0]
}
视图控制器:
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Make sure the array contains at least 1 item
guard arrayOne.count > 0 else {
return 0
}
// Return the number of items for this array
let currentArrayOne = arrayOne[currentArrayOneIndex!]
if currentArrayOne.tableCells != nil {
return currentArrayOne.tableCells!.count
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Get a cell
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell
// Get the instruction that the tableView is asking about
let tableLabel = cell.tableLabel
if tableLabel != nil {
let currentArrayOne = arrayOne[currentArrayOneIndex!]
if currentArrayOne.tableCells != nil && indexPath.row < currentArrayOne.tableCells!.count {
// Set the text for the label
tableLabel!.text = currentArrayOne.tableCells![indexPath.row]
}
}
// Return the cell
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
当我尝试cell.accessoryType
在 ViewController 中实现时,Xcode 无法识别accessoryType
. 我相信这是因为我的方法是 forUITableView
而不是UITableViewCell
,所以使用自定义单元格的额外皱纹给我带来了一些困惑。非常感谢任何指导!
解决方案
对不起,你的方法完全错误。永远不要将视图用作数据源类型。不。
创建一个模型,这是一个带有name
和isSelected
属性的简单示例
struct Model {
let name : String
var isSelected = false
}
并在控制器 ViewController
中声明一个包含Model
实例的数据源数组
var arrayOne = [Model(name: "Foo"), Model(name: "Bar"), Model(name: "Baz")]
在扩展中numberOfRows
只返回数组中的项目数(您的guard
表达式根本没有意义),根据cellForRow
设置复选标记isSelected
并didSelect
切换isSelected
属性并重新加载行。
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOne.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Get a cell
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell
// Get the item of the data source array
let item = arrayOne[indexPath.row]
// update the view
cell.tableLabel = item.name
cell.accessoryType = item.isSelected ? .checkmark : .none
// Return the cell
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// toggle isSelected of the row in the model
arrayOne[indexPath.row].isSelected.toggle()
// reload the row to update the view
tableView.reloadRows(at: [indexPath], with: .none)
}
}
在自定义单元格中只声明出口,没有别的
class TableCell: UITableViewCell {
@IBOutlet weak var tableLabel: UILabel!
}
这种方法的好处是模型和视图总是同步的。
推荐阅读
- python - 在 Django 中聚合具有多个 OuterRef 的子查询
- symfony - 如何在 Symfony 5 中从一个数据库表渲染表单并 POST 到另一个数据库表
- c# - 如何在 c# c sharp 中创建一个二维数组 [n,3] 从用户那里获取字符串输入
- ffmpeg - 使用 ffmpeg 创建一个黑色背景的音乐视频
- vimeo - 即使视频可用,也为 vimeo api 获取 404?
- image - Spring内容,如何在其他页面获取图片
- python - 提取包含列名或表条目中特定字符串的熊猫数据框
- azure-devops - Azure 开发运维。通过运行时变量定义变量组
- python-3.x - 如何在 Python 中将函数放入列表中?
- java - Android片段被另一个片段覆盖