我有多个部分的Tableview& tableview单元格中的许多数据。在我的情况下,只在一个部分中选择一个单选按钮。所以我们在一个部分选择单选按钮然后向下滚动并选择另一个部分,当我们来到所有单选按钮时未选中如何保存按钮状态。所以解决这个问题我们创建一个NSMutable数组来保存按钮状态,但是这个问题没有解决。我在swift中编写这段代码。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell=table_view.dequeueReusableCellWithIdentifier(identifier,forIndexPath: indexPath) as! tablecell
let manyCells : Bool = arrayForBool .objectAtIndex(indexPath.section).boolValue
if (!manyCells) {
}
else
{
cell.lbl_text.text = data[indexPath.section][indexPath.row]
Section=indexPath.section
cell.tag = indexPath.row + 1
cell.btn_radio.tag=indexPath.row
cell.btn_radio.selected = arr_Selection[indexPath.section][indexPath.row]
}
if indexPath.isEqual(selectedIndex)
{
cell.btn_radio.selected = arr_Selection[indexPath.section][indexPath.row]
}
else {
cell.btn_radio.selected = false
}
return cell
}
@IBAction func btn_radio(sender: AnyObject)
{
let Section_count = table_view.numberOfSections
let rowCount = table_view.numberOfRowsInSection(indexPath.section)
print("rowCount\(rowCount)")
for j in 0..<Section_count
{
if j==indexPath.section
{
for i in 0..<rowCount
{
if i==indexPath.row
{
selectedIndex = indexPath
arr_Selection[indexPath.section][indexPath.row]=true
let range = NSMakeRange(indexPath.section, 1)
let sectionToReload = NSIndexSet(indexesInRange: range)
table_view .reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.None)
}
}
}
}
}
这个代码是我用来选择它工作的按钮但是当在tableview中滚动时不保存按钮选择的状态如何解决这个问题。