Ios 点击另一个单元格后如何操作UITableViewCell?-敏捷的
点击另一个单元格后如何操作Ios 点击另一个单元格后如何操作UITableViewCell?-敏捷的,ios,swift,uitableview,Ios,Swift,Uitableview,点击另一个单元格后如何操作UITableViewCell 我有3个单元格,每个单元格都有UIPickerView第一个单元格的userInteractionEnabled为true,但第二个和第三个单元格的false。。当用户点击第一个单元格时,其余单元格的userInteractionEnabled应为true 我知道我需要使用userInteractionEnabled但是如何使用?我是否应该将单元格保存在变量中,然后在需要时进行操作 使用变量跟踪userInteractionEnabled
UITableViewCell
我有3个单元格,每个单元格都有UIPickerView
第一个单元格的userInteractionEnabled
为true
,但第二个和第三个单元格的false
。。当用户点击第一个单元格时,其余单元格的userInteractionEnabled
应为true
我知道我需要使用
userInteractionEnabled
但是如何使用?我是否应该将单元格保存在变量中,然后在需要时进行操作 使用变量跟踪userInteractionEnabled
var selectable: Bool = false
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = sections[indexPath.row]
if selectable {
cell.isUserInteractionEnabled = true
} else {
if indexPath.row != 0 { // Only first cell is enabled
cell.isUserInteractionEnabled = false
}
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
if indexPath.row == 0 {
selectable = true
tableView.reloadData()
}
}
您可以创建一个保持第一个项目的选择状态的变量:
var didSelectFirst = false
然后,在tableView(uquo:didSelectRowAtIndexPath:)
中,您可以告诉tableView完全重新加载,或者只重新加载要重新加载的两行
if indexPath.row == 0 {
didSelectFirst = true
// reload all
tableView.reloadData()
// reload some
tableView.reloadRowsAtIndexPaths([
NSIndexPath(forRow: 1, inSection: 0),
NSIndexPath(forRow: 2, inSection: 0)], withRowAnimation: .Automatic)
}
在tableView(cellforrowatinexpath:)
中,您可以使用didSelectFirst
变量更改userInteractionEnabled
if indexPath.row == 1 || indexPath.row == 2 {
cell.userInteractionEnabled = didSelectFirst
}
我已经阅读了上面的解决方案,当然它们都是有效的,但我更喜欢这样的解决方案:我假设您有一个要显示的对象数组(忽略您正在使用的模式) 我认为这个解决方案更具可扩展性和可维护性,但这是我的看法 更新 对于在“CellForRowatineXpath”函数之外操作单元格,您可以通过以下方式进行操作,例如:
func manuallyModifyCell(atIndex index: Int, backgroundColor: UIColor = .clearColor()) {
let indexPath = NSIndexPath(forRow: index, inSection: 0)
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.backgroundColor = backgroundColor
}
}
单击“表视图行”时是否希望使用类似tik标记的复选标记?在我看来,最好只重新加载已更新的单元格(请参阅我的答案)。否则,此解决方案基本上与我提出的解决方案相同。谢谢您,这是可行的,但仍然存在问题,因为在tableView(CellForRowatineXpath:)中,我使用了cell.backgroundColor=UIColor.clearColor(),那么现在如何使单元格再次变白?
cell.backgroundColor=UIColor.whiteColor()
会使单元格再次变白我希望它们在启用后再次变白如果didSelectFirst{cell.backgroundColor=UIColor.whiteColor()}
或类似的东西didSelectFirst
跟踪是否选择了第一个单元格。我知道这一点,但我的意思是在didselectrowatinexpath中如何操作“单元格”??我是否应该在DidSelectRowatineXpath中再次使用CellForRowatineXpath?谢谢您的回答,但您能否告诉我,我是否更改了CellForRowatineXpath中不可选择单元格的背景色。。如何在点击第一个单元格后使颜色再次变白?我的意思是当我在CellForRowatineXpath乐趣之外时如何操纵cell对象
func manuallyModifyCell(atIndex index: Int, backgroundColor: UIColor = .clearColor()) {
let indexPath = NSIndexPath(forRow: index, inSection: 0)
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.backgroundColor = backgroundColor
}
}