【问题标题】:panning cells with custom action in ios在 ios 中使用自定义操作平移单元格
【发布时间】:2014-01-24 00:17:06
【问题描述】:

嗨,我正在尝试使用邮箱样式平移 http://www.mailboxapp.com/ 制作一个表格视图,因为我正在使用这个库 https://github.com/gloubibou/HHPanningTableViewCell,它工作正常,我滑动单元格,它移动得很好,问题是我想要当我滑动单元格时触发自定义操作,我只能在它打开然后触摸它时执行它。

这是发生动作的代码

    #import "TableViewController.h"

#import "HHPanningTableViewCell.h"


@interface TableViewController ()

@property (nonatomic, retain) NSArray *rowTitles;

@end


@implementation TableViewController


#pragma mark -
#pragma mark Initialization

- (id)init
{

    self = [super initWithNibName:@"TableViewController" bundle:nil];

    if (self != nil) {
        self.rowTitles = [NSArray arrayWithObjects:@"Pan direction: None", @"Pan direction: Right", @"Pan direction: Left", @"Pan direction: Both", @"Custom trigger", nil];
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];



    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark -
#pragma mark Accessors

@synthesize rowTitles = _rowTitles;


#pragma mark -
#pragma mark Rotation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.rowTitles count] * 1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSInteger directionMask = indexPath.row % 5;


    if (cell == nil) {
        cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                              reuseIdentifier:CellIdentifier];

        UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];

        // dark_dotted.png obtained from http://subtlepatterns.com/dark-dot/
        // Made by Tsvetelin Nikolov http://dribbble.com/bscsystem
        drawerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark_dotted"]];

        cell.drawerView = drawerView;       
    }

    if (directionMask < 3) {
        cell.directionMask = directionMask;
    }
    else {
        cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight;

        if (directionMask == 4) {
            cell.delegate = self;
        }
    }

    cell.textLabel.text = [self.rowTitles objectAtIndex:directionMask];




    return cell;
}

- (void)gestureRecognizerDidPan:(UIPanGestureRecognizer*)gestureRecognizer{

}

#pragma mark -
#pragma mark Table view delegate



- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     NSInteger directionMask = indexPath.row;
    NSString *celda = [NSString stringWithFormat:@"%d", directionMask];

    [cell isKindOfClass:[HHPanningTableViewCell class]];
    HHPanningTableViewCell *panningTableViewCell = (HHPanningTableViewCell*)cell;


    if (directionMask == 1) {
        if (HHPanningTableViewCellDirectionRight) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"1"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        if ([panningTableViewCell isDrawerRevealed]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"1"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"2"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }


    return indexPath;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

#pragma mark -
#pragma mark HHPanningTableViewCellDelegate

- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                    message:@"You triggered a custom action"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

@end

我知道我可以使用手势识别器来触发动作,但我认为图书馆已经这样做了。

在这部分中,我触发了一个动作,知道单元格,它被平移到哪里以及单元格的背面是否显示,但总是通过单击它,因为它是一个选择功能。

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     NSInteger directionMask = indexPath.row;
    NSString *celda = [NSString stringWithFormat:@"%d", directionMask];

    [cell isKindOfClass:[HHPanningTableViewCell class]];
    HHPanningTableViewCell *panningTableViewCell = (HHPanningTableViewCell*)cell;


    if (directionMask == 1) {
        if (HHPanningTableViewCellDirectionRight) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"1"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        if ([panningTableViewCell isDrawerRevealed]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"1"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                            message:@"2"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }


    return indexPath;
} 

我相信这另一部分是应该触发自定义操作的地方,但程序从不进入这个函数

- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                    message:@"You triggered a custom action"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

我希望我说清楚了,提前谢谢你。

【问题讨论】:

    标签: ios uitableview panning


    【解决方案1】:

    将委托方法改为

    - (void)panningTableViewCell:(HHPanningTableViewCell *)cell didTriggerWithDirection:(HHPanningTableViewCellDirection)direction;
    

    【讨论】:

      【解决方案2】:

      要触发委托方法,您需要将控制器设置为单元的委托。目前在您的cellForRowAtIndexPath 中,仅当directionMask 为4 时,控制器才被分配为委托。因此,您要么在当前代码中将directionMask 设置为4(而是根据单元格位置返回一个值),要么设置就像我在下面所做的那样,控制器在每种情况下都作为委托。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *CellIdentifier = @"Cell";
          HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          NSInteger directionMask = indexPath.row % 5;
      
      
          if (cell == nil) {
              cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                    reuseIdentifier:CellIdentifier];
      
              UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];
      
              // dark_dotted.png obtained from http://subtlepatterns.com/dark-dot/
              // Made by Tsvetelin Nikolov http://dribbble.com/bscsystem
              drawerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark_dotted"]];
      
              cell.drawerView = drawerView;       
          }
      
          if (directionMask < 3) {
              cell.directionMask = directionMask;
          }
          else {
              cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight;
      
              // previous code
              //if (directionMask == 4) {
              //    cell.delegate = self;
              //}
          }
          cell.delegate = self;
      
          cell.textLabel.text = [self.rowTitles objectAtIndex:directionMask];
      
          return cell;
      }
      

      【讨论】:

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