【发布时间】:2020-03-24 12:08:33
【问题描述】:
我想限制我的 tableview 的某些部分只允许选择 1 个单元格,到目前为止,我的所有单元格都可以被选择,无论它位于哪个部分
但是有一点点扭曲:我的部分是一个 [array] 并且根据不同的变量动态变化。
我的部分本身就是一个变量,所以我可以像这样以编程方式精确定位它们:
var section1 = [NSDictionary(objects: [NSLocalizedString("Alcohol use less than 24 hours", comment:""), 2],
EDIT2:有人指出我可以创建一个包含限制的变量
var restrictedSections: [[NSDictionary]: Bool] {return [section1: true,section2: true,section3: true, section4: true, section4COPI: true, section5: true, section5COPI: true, section6: false, section7: false, section8: false] }
这不能被引用为 IndexPath,所以没有运气......但也许在正确的道路上?
tableView 中的一些代码(为 StackOverflow 读者简化):
// checkmarks when tapped
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
if self.selectedIndexPaths.contains(indexPath) {
cell.accessoryType = .none
cell.backgroundColor = UIColor.clear
self.selectedIndexPaths.remove(indexPath)
if CrewMembersNumber == "1" {
if((indexPath).section == 0) {
self.section1score -= section1[(indexPath).row].object(forKey: "value") as! Int
} else if((indexPath).section == 1) {
self.section3score -= section3[(indexPath).row].object(forKey: "value") as! Int
}
} else if CrewMembersNumber == "2" {
if((indexPath).section == 0) {
self.section1score -= section1[(indexPath).row].object(forKey: "value") as! Int
} else if((indexPath).section == 1) {
self.section2score -= section2[(indexPath).row].object(forKey: "value") as! Int
}
} else if CrewMembersNumber == "3" {
if((indexPath).section == 0) {
self.section1score -= section1[(indexPath).row].object(forKey: "value") as! Int
} else if((indexPath).section == 1) {
self.section5score -= section5[(indexPath).row].object(forKey: "value") as! Int
}
} else {
// if crewmemebernumber doest return 1-2 or 3
if((indexPath).section == 0) {
self.section1score -= section1[(indexPath).row].object(forKey: "value") as! Int
} else if((indexPath).section == 1) {
self.section4score -= section4[(indexPath).row].object(forKey: "value") as! Int
}
}
} else {
cell.accessoryType = .checkmark
cell.backgroundColor = UIColor (red:236/255.0, green: 236/255, blue: 236/255, alpha: 1.0)
self.selectedIndexPaths.add(indexPath)
if CrewMembersNumber == "1" {
if((indexPath).section == 0) {
self.section1score += section1[(indexPath).row].object(forKey: "value") as! Int
} else if((indexPath).section == 1) {
self.section3score += section3[(indexPath).row].object(forKey: "value") as! Int
}
} else if CrewMembersNumber == "2" {
if((indexPath).section == 0) {
self.section1score += section1[(indexPath).row].object(forKey: "value") as! Int
} else if((indexPath).section == 1) {
self.section2score += section2[(indexPath).row].object(forKey: "value") as! Int
}
} else if CrewMembersNumber == "3" {
if((indexPath).section == 0) {
self.section1score += section1[(indexPath).row].object(forKey: "value") as! Int
} else if((indexPath).section == 1) {
self.section5score += section5[(indexPath).row].object(forKey: "value") as! Int
}
} else {
// if crewmemebernumber doest return 1-2 or 3
if((indexPath).section == 0) {
self.section1score += section1[(indexPath).row].object(forKey: "value") as! Int
} else if((indexPath).section == 1) {
self.section4score += section4[(indexPath).row].object(forKey: "value") as! Int
}
}
}
self.updateToolbarAndLabel(self.totalScore)
self.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel!.text = self.textForIndexPath(indexPath);
cell.textLabel!.font = UIFont(name:"Avenir", size:19)
cell.textLabel!.numberOfLines = 0
cell.selectionStyle = UITableViewCellSelectionStyle.none;
if(self.selectedIndexPaths.contains(indexPath)) {
cell.accessoryType = .checkmark;
cell.backgroundColor = UIColor (red:236/255.0, green: 236/255, blue: 236/255, alpha: 1.0)
} else {
cell.accessoryType = .none;
cell.backgroundColor = UIColor.clear
}
return cell
}
正如您所见,当我点击一个单元格时,它会添加一个值,并且该单元格会更改其背景颜色并添加一个复选标记,
我需要做的是,如果有一个单元格被选中,在某些只能选择 1 个的部分中,它需要检查该部分中是否有任何单元格被选中并取消选择它以支持新的用户点击。现在我完全不明白如何做到这一点
感谢您的帮助
【问题讨论】:
-
你要保存选中行的部分,对吧
-
不是真的,除非要限制的部分需要这样做
-
嗯,我还是不明白第一个。
-
好吧,我有多个部分,这些部分将根据用户在表格打开之前的选择出现(或不出现),简单地说是部分名称:section4; section5 和 section4COPI section5COPI 需要被“标记”为受限,我相信最好的方法是创建一个“var”,列出其中哪些实际上显示在 tableview 中以限制它。除非有其他方法可以做到这一点
-
你的数据模型非常繁琐,效率低下。您应该使用自定义结构或类作为数据源数组(代表部分),其中包括代表行的项目。好处是您可以通过索引路径轻松引用任何对象。然后将
isSelected属性添加到反映选择状态的row 对象。这比使用额外的数组更有效且更可靠地维护。对于单选部分,获取当前选中的项目,将isSelected设置为false并在索引路径的项目中设置为true并重新加载两行。
标签: ios swift uitableview swift3