iOS开发Swift-UITableView-func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
作用:当选中此cell时需要执行的方法.
用于响应UITableView中某一行的选中事件。
当用户在UITableView中点击或轻击一行时,这个方法会被调用。在这个方法中,你可以编写代码来处理行选中事件,例如更新UI或进行其他操作。
该方法接收两个参数:
tableView
:表示发生选中事件的UITableView对象。indexPath
:表示被选中行的位置,是一个IndexPath对象。
你可以使用indexPath.row
属性来获取选中行的索引,然后根据需要进行相应的处理。例如,你可以根据选中行的索引从数据源中获取对应的数据,并在其他UI元素中显示这些数据。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // 获取选中行的索引 let selectedRow = indexPath.row // 从数据源中获取对应的数据 let selectedData = dataSource[selectedRow] // 在其他UI元素中显示选中的数据 otherUIElement.text = selectedData.title // 执行其他操作... }