我有一个设置屏幕,可以在两个单位系统(Metric和Imperial)之间进行选择,并且只想在用户选择特定单元格时显示一个复选标记,并在选择另一个单元格时切换复选标记。目前我的代码选择了所示的两个单元格,并且当我后来回到视图时,不会将复选标记保存到所选单元格。
关于如何解决这个问题的任何想法。我在堆栈上看到过类似的问题,但大多数都是在ObjC中。
代码:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
var weightValue: String? = unitsDefaults.objectForKey(kCCWeightKeyMetric) as! String?
var distanceValue: String? = unitsDefaults.objectForKey(kCCDistanceKeyMetric) as! String?
let cell = tableView.cellForRowAtIndexPath(indexPath)
if indexPath.row == 0 {
weightValue = "pounds" //Set to this for the default value of weight
unitsDefaults.setObject(weightValue, forKey: kCCWeightKeyMetric) //Save the kg value
distanceValue = "miles" //Set to this for the default value of distance
unitsDefaults.setObject(distanceValue, forKey: kCCDistanceKeyMetric) //Save the km value
cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
unitsDefaults.synchronize()
//Check mark Selection
if unitsDefaults.stringForKey(kCCDistanceKeyMetric)! == "miles" {
cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
} else if unitsDefaults.stringForKey(kCCDistanceKeyMetric)! == "km"{
cell?.accessoryType = UITableViewCellAccessoryType.None
}
} else {
weightValue = "kg" //Set to this for the default value of weight
unitsDefaults.setObject(weightValue, forKey: kCCWeightKeyMetric) //Save the kg value
distanceValue = "km" //Set to this for the default value of distance
unitsDefaults.setObject(distanceValue, forKey: kCCDistanceKeyMetric) //Save the km value
unitsDefaults.synchronize()
//Check mark Selection
if unitsDefaults.stringForKey(kCCDistanceKeyMetric)! == "km" {
cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
} else if unitsDefaults.stringForKey(kCCDistanceKeyMetric)! == "miles"{
cell?.accessoryType = UITableViewCellAccessoryType.None
}
}
//Dismiss the View
if let navController = self.navigationController {
navController.popViewControllerAnimated(true)
}
}
答案 0 :(得分:1)
这种类型。我在Objective-C写作。你可以获得Idea如何在Swift中执行。 创建一个可变字典,例如' cellDictionary'
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];
NSString *cellRowString =[NSString stringWithFormat:@"%ld",(long)indexPath.row];
if([cellDictionary valueForKey:cellRowString]!=nil){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellRowString =[NSString stringWithFormat:@"%ld",(long)indexPath.row];
if([cellDictionary valueForKey:cellRowString]==nil){
[cellDictionary setObject:cellRowString forKey:cellRowString];
}
[tableView reloadData];
}
答案 1 :(得分:0)
我不知道这个可能是你的问题。以防万一...不要在prepareforreuse(子类UITableViewCell)上重置附件。
-(void)prepareForReuse{
[super prepareForReuse];
self.accessoryType = UITableViewCellAccessoryNone;
}
答案 2 :(得分:0)
由于我使用的是静态细胞,我为这两个细胞创建了IBOutlets,并在View中检查确实加载了哪个锻炼。
@IBOutlet weak var firstCell: UITableViewCell!
@IBOutlet weak var secondCell: UITableViewCell!
var unitsDefaults = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
//Check mark Selection
if unitsDefaults.stringForKey(kCCDistanceKeyMetric)! == "miles" {
firstCell.accessoryType = UITableViewCellAccessoryType.Checkmark
secondCell.accessoryType = UITableViewCellAccessoryType.None
} else if unitsDefaults.stringForKey(kCCDistanceKeyMetric)! == "km"{
firstCell.accessoryType = UITableViewCellAccessoryType.None
secondCell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
....
}