【发布时间】:2014-09-01 09:04:03
【问题描述】:
我尝试每个部分只显示一个复选标记。因此,当用户在一个部分中选择一行时,应取消选择该行中最后选择的行,并用复选标记选择新行。我想处理这个非常动态和通用的方法,因为会有动态数量的部分。
到目前为止,我有以下内容:
if(self.product ? subMenus.group.selectedRow : subMenusInProductCart.group.selectedRow)
{
UITableViewCell *uncheckCell;
if (self.product) {
uncheckCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:subMenus.group.selectedRow.integerValue inSection:indexPath.section]];
if (uncheckCell.accessoryType == UITableViewCellAccessoryCheckmark) {
SubMenus *subMenu = [self.indexPathController.dataModel itemAtIndexPath:[NSIndexPath indexPathForRow:subMenus.group.selectedRow.integerValue inSection:indexPath.section]];
subMenu.selected = @(NO);
if (subMenus.price.floatValue > 0) {
self.totalPrice = [self subtractSubMenuPrice:subMenus.price.floatValue fromProductPrice:self.totalPrice];
}
}
}else if (self.cartProduct) {
uncheckCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:subMenusInProductCart.group.selectedRow.integerValue inSection:indexPath.section]];
if (uncheckCell.accessoryType == UITableViewCellAccessoryCheckmark) {
SubMenuProductCart *subMenuProductCart = [self.indexPathController.dataModel itemAtIndexPath:[NSIndexPath indexPathForRow:subMenus.group.selectedRow.integerValue inSection:indexPath.section]];
subMenuProductCart.selected = @(NO);
if (subMenuProductCart.subMenu.price.floatValue > 0) {
self.totalPrice = [self subtractSubMenuPrice:subMenuProductCart.subMenu.price.floatValue fromProductPrice:self.totalPrice];
}
}
}
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if ([subMenus.group.selectedRow isEqualToNumber:@(indexPath.row)])
{
if (self.product) {
subMenus.selected = @(NO);
subMenus.group.selectedRow = nil;
if (subMenus.price.floatValue > 0) {
self.totalPrice = [self subtractSubMenuPrice:subMenus.price.floatValue fromProductPrice:self.totalPrice];
}
}
}else {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if (self.product) {
subMenus.selected = @(YES);
subMenus.group.selectedRow = @(indexPath.row);
if (subMenus.price.floatValue > 0) {
self.totalPrice = [self addSubMenuPrice:subMenus.price.floatValue toProductPrice:self.totalPrice];
}
}else if (self.cartProduct) {
subMenusInProductCart.selected = @(YES);
subMenusInProductCart.group.selectedRow = @(indexPath.row);
if (subMenusInProductCart.subMenu.price.floatValue > 0) {
self.totalPrice = [self addSubMenuPrice:subMenusInProductCart.subMenu.price.floatValue toProductPrice:self.totalPrice];
}
}
}
上面的代码是有效的,但我认为它们一定是一个更好的解决方案,而不是在 Core Data 中保存选定的行?请记住,选择的状态将在以后被操纵,因为用户可以更改他或她的选择(购物车)。当前,选定对象(行)被传递给具有独立选定行属性的另一个对象(关系对象)。
为了说明我的设置:
那么有没有一种更好、更通用的方法来使用 Core Data 对象处理每个部分的这一选择,而不是使用 NSMangedObject 保存选定的行,这似乎不太 MVC'ish?
【问题讨论】:
标签: ios objective-c core-data model-view-controller