【问题标题】:How do I make reorder control of UITableViewCell in left side?如何在左侧对 UITableViewCell 进行重新排序控制?
【发布时间】:2012-06-21 15:50:55
【问题描述】:

我正在做一个新闻阅读器应用程序,我想让它让用户可以选择显示/隐藏新闻类别(如热门新闻、商业、技术、体育等)并像 BBC 新闻应用程序一样重新排序它们安卓。

见下图:

我的问题是:

  • 如何在单元格左侧进行重新排序控制? (其默认位置在编辑模式下的单元格右侧)
  • 我有一个复选框自定义控件。如何将其放在单元格的右侧?

【问题讨论】:

    标签: ios uitableview custom-controls


    【解决方案1】:

    回答 [如果您不使用任何配件(细节披露等)]

    (0) 我假设您已经设置好了表格,等等。

    (1) 此解决方案仅适用于您的表格单元格始终可拖动的情况。将此添加到您的 viewDidLoad 到您的 Table View Controller .m 文件中。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self setEditing:YES];
    }
    

    (2) 要重新排序单元格,请将 cell.showsReorderControl = YES; 添加到 tableView:cellForRowAtIndexPath:。

    (3) 确保您拥有 tableView:canMoveRowAtIndexPath: 和 tableView:moveRowAtIndexPath:toIndexPath: 方法。

    - (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES; 
    }
    
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    {
    }
    

    (4) 因为你想要左侧的重新排序控件,所以我们通常必须去掉那里的 Delete 圆圈,而 tableView:editingStyleForRowAtIndexPath: 方法可以做到这一点。

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewCellEditingStyleNone;
    }
    

    (5) 最后一步,魔法发生的地方 - 添加 tableView:willDisplayCell:forRowAtIndexPath: 方法,搜索单元格的子视图,将其缩小到私有 UITableViewCellReorderControl,最后覆盖它。

    - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        for(UIView* view in cell.subviews)
        {
            if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
            {
                // Creates a new subview the size of the entire cell
                UIView *movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
                // Adds the reorder control view to our new subview
                [movedReorderControl addSubview:view];
                // Adds our new subview to the cell
                [cell addSubview:movedReorderControl];
                // CGStuff to move it to the left
                CGSize moveLeft = CGSizeMake(movedReorderControl.frame.size.width - view.frame.size.width, movedReorderControl.frame.size.height - view.frame.size.height);
                CGAffineTransform transform = CGAffineTransformIdentity;
                transform = CGAffineTransformTranslate(transform, -moveLeft.width, -moveLeft.height);
                // Performs the transform
                [movedReorderControl setTransform:transform];
            }
        }
    }
    

    我花了将近十个小时试图找出一个解决方案,当我无法解决配件时。我首先需要更多的经验和知识。简单地对公开按钮上的重新排序控件做同样的事情是行不通的。所以我希望这暂时有效;如果我以后弄明白了,我一定会更新这个。

    嗨 Hoang Van Ha,这是我第一次回答,我一个月前才开始学习 Objective-C,所以我还是个菜鸟。

    我一直在尝试对我的应用程序做同样的事情,并且一直在寻找如何做到这一点。在这方面,Apple 的文档并没有太大帮助。当人们只是链接到文档时,我发现它非常令人沮丧。我想对他们大喊大叫。希望我的回答对你有所帮助。

    对b2Cloud 赞不绝口,因为我使用了来自this article 的一些代码并将其改编为我的答案。

    【讨论】:

    • 了不起的卢克先生......我需要左侧的删除控件和重新排序控件,这可能吗?
    • 你知道如何使整个单元格成为重新排序控件,这样你就不必抓住小图标了吗?
    • 这必须是公认的答案。我已经搜索了一段时间了,效果很好。谢谢卢克。
    • 我遇到了这个解决方案的问题,函数 willDisplayCell 可以为同一个单元格多次调用,例如,当您将单元格滚动出窗口然后滚动到里面时。当这种情况发生并且您再次调用转换时,图标会消失在左侧。我解决这个问题的方法是,我没有使用变换,而是将新视图的 Y 位置设置为固定位置:CGRect newFrame =movedReorderControl.frame; newFrame.origin.x = -220; moveReorderControl.frame = newFrame;
    • 我会指出@David 提出的问题也会导致将多个 UIView 添加到单元格中。每次滚动 TableView 时都会发生这种情况。这是由于创建了不再使用但仍然存在的视图而导致的内存泄漏。我已经对单元格进行了子类化,所以我添加了一个属性来跟踪创建的视图并在 willDisplayCell 中重用它。
    【解决方案2】:

    与其寻找UITableViewCellReorderControl,这是非公共API,将来可能会改变,我建议使用UILongPressGestureRecognizer 而不是UITableView,并实现您自己的重新排序逻辑。

    我解决了HPReorderTableView 中的一般情况(响应单元格任何部分的长按),这是 UITableView 的直接替换。可以轻松修改它以响应对单元格特定部分的触摸,例如通过实现 reorderGestureRecognizer 委托的 gestureRecognizer:shouldReceiveTouch:。

    【讨论】:

    • 请注意,这家伙拥有 23.7k 声望是有原因的。其他答案是拙劣的,将来会回来咬你。另外请记住,Apple 发布 UI 指南并尝试遵守它们是有充分理由的 - 用户期望此控件位于右侧。
    • 这是真的。 UITableView 的编辑行为是不可定制的,原因是它在历史上没有,现在他们不能添加它。相反,它们具有更通用的 UICollectionView,它可以轻松地作为 UITableView 的替代品,并允许您自定义其行为。
    【解决方案3】:

    现在 iOS 9 及更高版本有一个更简单的解决方案

    tableView.semanticContentAttribute = .forceRightToLeft
    

    编辑:

    正如Roland 提到的,您应该注意到“如果您通过leading 和trailing 锚而不是left 和right 对齐单元格的子视图,则此设置将翻转所有内容。”

    【讨论】:

    • 如果您通过leading 和trailing 锚而不是left 和right 对齐单元格的子视图,则此设置将翻转所有内容。
    • 它还在运动控制和单元格内容之间添加了一些愚蠢的空间......
    • @Starwave 你找到解决这个问题的方法了吗?
    【解决方案4】:

    Luke Dubert,对 iOS 7 有很酷的答案,应该稍微改一下:

    UIView *cellSubview = cell.subviews[0];
        for(UIView* view in cellSubview.subviews)
        {
            if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
            {
    

    【讨论】:

      【解决方案5】:

      我使用 swift 4.2 在 iOS 12 上进行了测试,而不是使用 view.self.description == "UITableViewCellReorderControl",而是使用 view.self.description.contains("UITableViewCellReorderControl")

      func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
          for view in cell.subviews {
              if view.self.description.contains("UITableViewCellReorderControl") {
                  let movedReorderControl = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.maxX, height: view.frame.maxY))
                  movedReorderControl.addSubview(view)
                  cell.addSubview(movedReorderControl)
                  let moveLeft = CGSize(width: movedReorderControl.frame.size.width - view.frame.size.width, height: movedReorderControl.frame.size.height - view.frame.size.height)
                  var transform: CGAffineTransform = .identity
                  transform = transform.translatedBy(x: -moveLeft.width, y: -moveLeft.height)
                  movedReorderControl.transform = transform
              }
          }
      }
      

      【讨论】:

      • 我正在寻找一种方法来调整重新排序控件的位置。不,我不想把它一直移到左边,但我能够使用你的代码来完成我需要的事情。谢谢!
      【解决方案6】:

      受卢克解决方案的启发,我找到了一种可靠且直接的方法来实现这一点:

      @implementation CustomTableViewCell
      
      -(void) layoutSubviews {
          [super layoutSubviews];
      
          // set the frames of the other subviews here if necessary, for example: 
          // self.textLabel.frame = CGRectMake(80, 0, self.textLabel.frame.size.width, self.textLabel.frame.size.height);
      
          for (UIView* view in self.subviews) {
              if ([[[view class] description] containsString:@"UITableViewCellReorderControl"]) {
                  [view setFrame:CGRectMake(20, 0, view.frame.size.width, view.frame.size.height)];
              }
          }
      }
      
      @end
      

      只需创建一个自定义 UITableViewCell,覆盖其子视图并修改每个视图的位置,即可在所有已知场景中正常工作。

      在 willDisplayCellAtIndexPath 下执行 UITableViewReorderControl 自定义无法 100% 工作,第一个重新排序按钮偶尔不会正确定位。

      【讨论】:

        【解决方案7】:

        这个解决方案在 iOS7 中对我不起作用,所以我为这两个版本创建了一个版本,我希望它有用:

        - (void)moveReorderControl:(UITableViewCell *)cell subviewCell:(UIView *)subviewCell
        {
            if([[[subviewCell class] description] isEqualToString:@"UITableViewCellReorderControl"]) {
            static int TRANSLATION_REORDER_CONTROL_Y = -20;
        
                //Code to move the reorder control, you change change it for your code, this works for me
                UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,    CGRectGetMaxX(subviewCell.frame), CGRectGetMaxY(subviewCell.frame))];
                [resizedGripView addSubview:subviewCell];
                [cell addSubview:resizedGripView];
        
                //  Original transform
                const CGAffineTransform transform = CGAffineTransformMakeTranslation(subviewCell.frame.size.width - cell.frame.size.width, TRANSLATION_REORDER_CONTROL_Y);
                //  Move custom view so the grip's top left aligns with the cell's top left
        
                [resizedGripView setTransform:transform];
           }
        }
        
        //This method is due to the move cells icons is on right by default, we need to move it.
        - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if (tableView.tag == MEETING_BLOCK_TABLE_TAG) {
                for(UIView* subviewCell in cell.subviews)
                {
                    if ([ROCFunctions isIOS7]) {
                        if([[[subviewCell class] description] isEqualToString:@"UITableViewCellScrollView"]) {
                            for(UIView* subSubviewCell in subviewCell.subviews) {
                                [self moveReorderControl:cell subviewCell:subSubviewCell];
                            }
                        }
                    }
                    else{
                        [self moveReorderControl:cell subviewCell:subviewCell];
                    }
                }
            }
        }
        

        【讨论】:

          【解决方案8】:

          感谢以上所有人。这是我的解决方案。已经在iOS7和iOS9上测试过

          @implementation StoryPreviewCell
          -(void)layoutSubviews{
              [super layoutSubviews];
          
              //subviews contains first level and second level views. 
              NSMutableArray *subviews = [NSMutableArray array];
              [subviews addObjectsFromArray:self.subviews];
              for (NSInteger i=0; i<self.subviews.count; i++) {
                  UIView *firstLevelView = self.subviews[i];
                  if(firstLevelView.subviews.count) [subviews addObjectsFromArray:firstLevelView.subviews];
              }
          
              for(UIView* view in subviews) {
                  if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"]) {
                      UIView *reorderControl = view;
                      CGFloat leftMoveDistance = 100;//can customize
                      CGAffineTransform transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -leftMoveDistance, 0);
                      reorderControl.transform = transform;
                  }
              }
          }
          @end
          

          【讨论】:

            【解决方案9】:

            对于那些使用 Xamarin 的人,这是我基于 @luke-dubert 回答的解决方案,但在重用时要注意删除额外添加的视图:

            在 TableViewSource 上:

            public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
            {
                (cell as GraphBarLineCell)?.SetEditionMode(tableView.Editing);
            }
            

            在单元格上:

            private UIView _reorderControlContainer;
            
            public void SetEditionMode(bool editing)
                {
                    _title.Frame = editing ? TitleFrameForEditing : TitleFrame;
            
                    if (editing)
                    {
                        var reorderControl = Subviews.FirstOrDefault(x => x.Class.Name.Equals("UITableViewCellReorderControl"));
            
                        if (reorderControl != null)
                        {
                            _reorderControlContainer?.RemoveFromSuperview();
            
                            // Creates a new subview the size of the entire cell
                            _reorderControlContainer = new UIView(new CGRect(0, 0, reorderControl.Frame.GetMaxX(), reorderControl.Frame.GetMaxY()));
            
                            // Adds the reorder control view to our new subview
                            _reorderControlContainer.AddSubview(reorderControl);
            
                            // Adds our new subview to the cell
                            AddSubview(_reorderControlContainer);
            
                            // CGStuff to move it to the left
                            var moveLeft = new CGSize(_reorderControlContainer.Frame.Size.Width - reorderControl.Frame.Size.Width,
                                _reorderControlContainer.Frame.Size.Height - reorderControl.Frame.Size.Height);
            
                            var transform = CGAffineTransform.MakeIdentity();
                            transform = CGAffineTransform.Translate(transform, -moveLeft.Width, -moveLeft.Height);
                            _reorderControlContainer.Transform = transform;
            
                            // Align the icon with the title
                            var icon = reorderControl.Subviews[0];
                            icon.Frame = new CGRect(10, 25, icon.Frame.Width, icon.Frame.Height);
                        }
                    }
                }
            

            【讨论】:

              【解决方案10】:

              我找到了一个更简单的解决方案,它不需要对任何东西进行绝对的手动定位,甚至可以使用从/到编辑模式的动画过渡。应该适用于 iOS6 和 7。

              在某处的 UIView 上定义这个类别:

              @interface UIView (ClassSearch)
              
              - (instancetype)subviewOfClassMatching:(NSString*)partialName;
              
              @end
              
              @implementation UIView (ClassSearch)
              
              -(instancetype)subviewOfClassMatching:(NSString *)partialName
              {
                  if ([[[self class] description] rangeOfString:partialName options:NSCaseInsensitiveSearch].location != NSNotFound) {
                      return self;
                  }
              
                  for (UIView* v in self.subviews) {
                      id match = [v subviewOfClassMatching:partialName];
                      if (match) {
                          return match;
                      }
                  }
              
                  return nil;
              }
              
              @end
              

              然后,在您的 UITableViewCell 子类中,像这样覆盖 willTransitionToState:

              -(void)willTransitionToState:(UITableViewCellStateMask)state
              {
                  [super willTransitionToState:state];
              
                  if (state == UITableViewCellStateShowingEditControlMask) {
                      UIView* reorderControl = [self subviewOfClassMatching:@"ReorderControl"];
                      UIView* reorderContainer = [[UIView alloc] initWithFrame:self.bounds];
                      CGAffineTransform t = CGAffineTransformMakeScale(-1, 1);
                      reorderContainer.transform = t;
                      [self addSubview:reorderContainer];
                      [reorderContainer addSubview:reorderControl];
                  }
              }
              

              请注意,单元格需要配置为仅显示重新排序控件,否则需要做更多工作。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2011-05-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多