【问题标题】:One check mark per section每个部分一个复选标记
【发布时间】: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


    【解决方案1】:

    如果selectedRow 是您的数据模型中有意义的部分,我认为将选定状态保存在 Core Data 中没有问题。我不确定selectedselectedRow 属性之间的关系是什么,但理想情况下您只使用其中一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 2020-04-05
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多