【问题标题】:Reveal view when swiping a prototype table view cell滑动原型表视图单元格时显示视图
【发布时间】:2014-06-16 23:51:07
【问题描述】:

我想要做的是在UITableViewCell 上滑动时显示隐藏菜单。 我不知道该怎么做,任何指导将不胜感激。

这是一张图片,让你有一个想法:

Image http://imageshack.com/a/img855/9134/j6k8.png

【问题讨论】:

    标签: ios objective-c uitableview xcode5


    【解决方案1】:

    你可以通过使用自定义单元格来做到这一点,我给出了 ruff 的想法和示例代码如何实现这一点, 首先创建一个新文件objective-c,并将其命名为CustomCell的子类UITableViewCellCustomCell.h文件

    我采取了一些虚拟视图和标签

     #import <UIKit/UIKit.h>
    
    @interface CustomCell : UITableViewCell
    {
    
    }
    @property (nonatomic, retain) UIView *optionsView; //this is your options to show when swiped
    @property (nonatomic, retain) UIView *mainVIew; //this is your view by default present on top of options view
    @property (nonatomic, retain) UILabel *messageLabel;
    @property (nonatomic, retain) UILabel *optionsLabel;
    @end
    


    CustomCell.m

     #import "CustomCell.h"
    
     @implementation CustomCell
    
     - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
     {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
           //since we are not using the xib we hav to initilise hear
           _optionsView = [[UIView alloc]init];
           _mainVIew    = [[UIView alloc]init];
    
    
           _optionsView.backgroundColor = [UIColor greenColor];
           _mainVIew.backgroundColor    = [UIColor redColor];
    
           _optionsView.alpha = 0.0f;
           _mainVIew.alpha     = 1.0f;
    
    
           _messageLabel = [[UILabel alloc]init];
           _optionsLabel = [[UILabel alloc] init];
    
           [_optionsView addSubview:_optionsLabel]; //hear u can add image view or buttons to options view i just added the label
           [_mainVIew addSubview:_messageLabel];
    
           //add the gesture to main view
           UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(whenSwiped:)];
           swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
           [_mainVIew addGestureRecognizer:swipeGesture];//add it to main view
    
    
           UISwipeGestureRecognizer *swipeReverse = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(whenSwipedReversed:)];
           swipeReverse.direction = UISwipeGestureRecognizerDirectionLeft;
           [_optionsView addGestureRecognizer:swipeReverse]; //add it to options view so that user can swipe back
    
    
    
            [self.contentView addSubview:_optionsView]; //first add the optionsVIew
            [self.contentView addSubview:_mainVIew]; //then main view
    
         }
         return self;
       }
    
     - (void)setSelected:(BOOL)selected animated:(BOOL)animated
      {
        [super setSelected:selected animated:animated];
    
       // Configure the view for the selected state
      }
    
      - (void)layoutSubviews
     {
        [super layoutSubviews];
        _optionsView.frame = self.bounds;
        _mainVIew.frame    = self.bounds;
    
        _messageLabel.frame = _mainVIew.bounds;
        _optionsLabel.frame = _optionsView.bounds;
    
        _optionsView.alpha = 0.0f;
       _mainVIew.alpha    = 1.0f;
    
     }
    
      //handle swipe call backs in cell only and make a delegate to controller about the button actions of options view
     - (void)whenSwiped:(id)sender
      {
        CGRect frameRect = _mainVIew.frame;
        frameRect.origin.x = 300.0f;
        [UIView animateWithDuration:0.5 animations:^{
          _mainVIew.frame = frameRect;
          _optionsView.alpha = 1.0f;
        }];
      }
    
     - (void)whenSwipedReversed:(id)sender
      {
         CGRect frameRect = _mainVIew.frame;
         frameRect.origin.x = 0.0f;
         [UIView animateWithDuration:0.5 animations:^{
            _mainVIew.frame = frameRect;
            _optionsView.alpha = 0.0f;
          }];
      }
    
      @end
    

    在视图控制器中导入#import "CustomCell.h"

    在tableview的datasource方法中

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
       CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
       if(Cell == nil)
       {
           Cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
       }
       Cell.selectionStyle = UITableViewCellSelectionStyleNone;
       Cell.optionsLabel.text = @"options";
       Cell.messageLabel.text = @"swipe me";
    
       return Cell;
    
     }
    

    以上是示例,希望对您有所帮助.. :) 如果没有得到,请发表评论

    【讨论】:

    • 这正是我所需要的,我将它与我已经拥有的 CustomCells 一起使用并添加了手势并且它起作用了。真的谢谢
    【解决方案2】:

    以下是一些指导:

    1. 每个 UITableViewCell 可以有两个视图,一个在另一个之上。最顶部的视图是默认显示的,最底部的视图是您所谓的“隐藏菜单”。

    2. 您需要在 Storyboard 中注册一个 UISwipeGestureRecognizer,用于管理上面显示的表格单元格。您将在自定义 UITableViewCell 类中为该手势识别器创建一个 IBAction。在该操作中,您将处理发生的滑动,然后将单元格的最顶部视图移动您想要的量,直到最大位移(在 x 方向上)。

    如果您需要更多信息,请告诉我,我会提供更多信息。我无法从您最初的问题中看出您的经验如何,例如,您是否知道如何使用自定义 UITableViewCells 创建 UITableViews?

    更新:

    确保您正在创建一个包含自定义 UITableViewCell 的 XIB 文件。然后,您可以轻松地将 UISwipeGestureRecognizer 添加到 XIB,并将其连接到单元类中的 IBAction。在您的 UIViewController 中,您将使用重用标识符注册 XIB 并以这种方式填充您的 UITableView。

    【讨论】:

    • 是的,我知道如何使用自定义 UITableViewCells 创建 UITableView,但我不知道如何注册 UISwipeGEstureRecognizer,因为当我将手势添加到 UITableViewCell 时,我收到一条错误消息,提示我无法将 UISwipeGEstureRecognizer 添加到原型单元格...如果可以的话,我需要更多帮助来滑动隐藏的 UIView 并推送正常的 UIView。
    • 我将编辑我的答案以提供更多背景信息。
    • 抱歉我的无知,XIB是什么?
    • 一个简单的谷歌搜索将产生许多有用的回答这个问题。但我的猜测是,您可能需要先阅读一些 iOS 开发知识,然后再尝试使用该菜单完成您想要做的事情。
    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    相关资源
    最近更新 更多