KZSwipeTableViewCell 2.0.0

KZSwipeTableViewCell 2.0.0

测试已测试
Lang语言 SwiftSwift
许可 MIT
发布最后发布2016年2月
SPM支持 SPM

Kesi Maduka 维护。



KZSwipeTableViewCell

一个方便的 UITableViewCell 子类,实现了可滑动内容以触发操作(KZSwipeTableViewCell Swift 端口)

展示如何实现UITableViewCell,就像我们在执行得很好的 Mailbox iOS应用中看到的那样。

预览

退出模式

滑动单元格应该使其消失。在破坏模式中很方便。

切换模式

选择一个状态后,单元格会弹回,这样可以保留单元格。快速切换选项很方便。

用法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    KZSwipeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[KZSwipeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        // Remove inset of iOS 7 separators.
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            cell.separatorInset = UIEdgeInsetsZero;
        }

        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

        // Setting the background color of the cell.
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }

    // Configuring the views and colors.
    UIView *checkView = [self viewWithImageName:@"check"];
    UIColor *greenColor = [UIColor colorWithRed:85.0 / 255.0 green:213.0 / 255.0 blue:80.0 / 255.0 alpha:1.0];

    UIView *crossView = [self viewWithImageName:@"cross"];
    UIColor *redColor = [UIColor colorWithRed:232.0 / 255.0 green:61.0 / 255.0 blue:14.0 / 255.0 alpha:1.0];

    UIView *clockView = [self viewWithImageName:@"clock"];
    UIColor *yellowColor = [UIColor colorWithRed:254.0 / 255.0 green:217.0 / 255.0 blue:56.0 / 255.0 alpha:1.0];

    UIView *listView = [self viewWithImageName:@"list"];
    UIColor *brownColor = [UIColor colorWithRed:206.0 / 255.0 green:149.0 / 255.0 blue:98.0 / 255.0 alpha:1.0];

    // Setting the default inactive state color to the tableView background color.
    [cell setDefaultColor:self.tableView.backgroundView.backgroundColor];

    [cell.textLabel setText:@"Switch Mode Cell"];
    [cell.detailTextLabel setText:@"Swipe to switch"];

    // Adding gestures per state basis.
    [cell setSwipeGestureWithView:checkView color:greenColor mode:KZSwipeTableViewCellModeSwitch state:KZSwipeTableViewCellState1 completionBlock:^(KZSwipeTableViewCell *cell, KZSwipeTableViewCellState state, KZSwipeTableViewCellMode mode) {
        NSLog(@"Did swipe \"Checkmark\" cell");
    }];

    [cell setSwipeGestureWithView:crossView color:redColor mode:KZSwipeTableViewCellModeSwitch state:KZSwipeTableViewCellState2 completionBlock:^(KZSwipeTableViewCell *cell, KZSwipeTableViewCellState state, KZSwipeTableViewCellMode mode) {
        NSLog(@"Did swipe \"Cross\" cell");
    }];

    [cell setSwipeGestureWithView:clockView color:yellowColor mode:KZSwipeTableViewCellModeSwitch state:KZSwipeTableViewCellState3 completionBlock:^(KZSwipeTableViewCell *cell, KZSwipeTableViewCellState state, KZSwipeTableViewCellMode mode) {
        NSLog(@"Did swipe \"Clock\" cell");
    }];

    [cell setSwipeGestureWithView:listView color:brownColor mode:KZSwipeTableViewCellModeSwitch state:KZSwipeTableViewCellState4 completionBlock:^(KZSwipeTableViewCell *cell, KZSwipeTableViewCellState state, KZSwipeTableViewCellMode mode) {
        NSLog(@"Did swipe \"List\" cell");
    }];

    return cell;
}

委托

KZSwipeTableViewCell 有一些委托方法,用于跟踪用户行为。请查看头文件,以了解 KZSwipeTableViewCellDelegate 提供的所有方法。

@interface MCTableViewController () <KZSwipeTableViewCellDelegate>
#pragma mark - KZSwipeTableViewCellDelegate

// Called when the user starts swiping the cell.
- (void)swipeTableViewCellDidStartSwiping:(KZSwipeTableViewCell *)cell;

// Called when the user ends swiping the cell.
- (void)swipeTableViewCellDidEndSwiping:(KZSwipeTableViewCell *)cell;

// Called during a swipe.
- (void)swipeTableViewCell:(KZSwipeTableViewCell *)cell didSwipeWithPercentage:(CGFloat)percentage;

单元格删除

KZSwipeTableViewCellModeExit 模式下,您可能会想要使用漂亮的淡入淡出动画删除单元格,以下行将为您提供一个执行的例子

[cell setSwipeGestureWithView:crossView color:redColor mode:KZSwipeTableViewCellModeExit state:KZSwipeTableViewCellState2 completionBlock:^(KZSwipeTableViewCell *cell, KZSwipeTableViewCellState state, KZSwipeTableViewCellMode mode) {
    NSLog(@"Did swipe \"Cross\" cell");

    // Code to delete your cell...

}];

您还可以在删除单元格之前请求确认

__weak MCTableViewController *weakSelf = self;

[cell setSwipeGestureWithView:crossView color:redColor mode:KZSwipeTableViewCellModeExit state:KZSwipeTableViewCellState1 completionBlock:^(KZSwipeTableViewCell *cell, KZSwipeTableViewCellState state, KZSwipeTableViewCellMode mode) {
    NSLog(@"Did swipe \"Cross\" cell");

    __strong MCTableViewController *strongSelf = weakSelf;
    strongSelf.cellToDelete = cell;

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Delete?"
                                                        message:@"Are you sure your want to delete the cell?"
                                                       delegate:self
                                              cancelButtonTitle:@"No"
                                              otherButtonTitles:@"Yes", nil];
    [alertView show];
}];

然后处理 UIAlertView 的操作

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    // No
    if (buttonIndex == 0) {
        [_cellToDelete swipeToOriginWithCompletion:^{
            NSLog(@"Swiped back");
        }];
        _cellToDelete = nil;
    }

    // Yes
    else {
        // Code to delete your cell...
    }
}

在示例项目中也有一个例子,我建议您查看。

更改触发百分比

如果默认触发限制不适合您的需求,您可以使用 firstTrigger 属性 (默认值:25%)secondTrigger 属性 (默认值:75%) 来更改它们。

cell.settings.firstTrigger = 0.1;
cell.settings.secondTrigger = 0.5;

重置单元格位置

使用 KZSwipeTableViewCellModeExit 模式结合 -swipeToOriginWithCompletion: 方法可以将单元格恢复到其原始位置。

[cell swipeToOriginWithCompletion:^{
    NSLog(@"Cell swiped back!");
}];

安装

联系

Kesi Maduka

原作者

Ali Karagoz

许可证

KZSwipeTableViewCell 以 MIT 许可证可用。有关更多信息,请参阅 LICENSE 文件。