【发布时间】:2014-06-16 23:51:07
【问题描述】:
我想要做的是在UITableViewCell 上滑动时显示隐藏菜单。
我不知道该怎么做,任何指导将不胜感激。
这是一张图片,让你有一个想法:
【问题讨论】:
标签: ios objective-c uitableview xcode5
我想要做的是在UITableViewCell 上滑动时显示隐藏菜单。
我不知道该怎么做,任何指导将不胜感激。
这是一张图片,让你有一个想法:
【问题讨论】:
标签: ios objective-c uitableview xcode5
你可以通过使用自定义单元格来做到这一点,我给出了 ruff 的想法和示例代码如何实现这一点,
首先创建一个新文件objective-c,并将其命名为CustomCell的子类UITableViewCell和CustomCell.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;
}
以上是示例,希望对您有所帮助.. :) 如果没有得到,请发表评论
【讨论】:
以下是一些指导:
每个 UITableViewCell 可以有两个视图,一个在另一个之上。最顶部的视图是默认显示的,最底部的视图是您所谓的“隐藏菜单”。
您需要在 Storyboard 中注册一个 UISwipeGestureRecognizer,用于管理上面显示的表格单元格。您将在自定义 UITableViewCell 类中为该手势识别器创建一个 IBAction。在该操作中,您将处理发生的滑动,然后将单元格的最顶部视图移动您想要的量,直到最大位移(在 x 方向上)。
如果您需要更多信息,请告诉我,我会提供更多信息。我无法从您最初的问题中看出您的经验如何,例如,您是否知道如何使用自定义 UITableViewCells 创建 UITableViews?
更新:
确保您正在创建一个包含自定义 UITableViewCell 的 XIB 文件。然后,您可以轻松地将 UISwipeGestureRecognizer 添加到 XIB,并将其连接到单元类中的 IBAction。在您的 UIViewController 中,您将使用重用标识符注册 XIB 并以这种方式填充您的 UITableView。
【讨论】: