SNLInteractionTableView 1.3.4

SNLInteractionTableView 1.3.4

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2018年1月

Simon Nickel 维护。




SNLInteractionTableView 提供了一个完整的 tableView 栈(控制器、tableView 和 cell),以轻松地将更多交互添加到您的 tableView 中。它使用 AutoLayout,并从您的 Storyboard 扩展现有的TableViewCell布局,具有以下功能

  • 滑动操作 - 向左、向右,带有弹跳、滑动回或滑动退出动画
  • 选择 - 带有工具栏
  • 重新排序 - 长按

此仓库包含 SNLInteractionTableView 和一个示例项目,以展示您可以如何使用它。有关详细说明,请参阅以下 说明 或下方的使用部分。

您可以完全免费使用此代码,没有任何限制,无论您想要什么。即使您真的想将其打印出来。如果您这样做(使用,而不仅仅是打印),将非常高兴听到您的消息。只需 tweet 在 @simonnickel 或通过电子邮件(见个人资料)发给我。

重新排序功能是受到以下内容的启发/重建/复制:Ben Vogelzang 的 BVReorderTableView。如果您只想进行重新排序:请使用他的代码!

Example

安装

使用

有关更多详细信息,请参阅 InteractionTableViewExample 中的示例项目。

  1. 使用 CocoaPods 进行安装。
  2. 将您的 Storyboard 中的 Controller、TableView 和 TableViewCell 类更改为相关的 SNInteraction 类,或者将现有的自定义类作为相关 SNInteraction 类的子类。
  3. 在您的 TableViewController 中设置-cell 在 tableView:cellForRowAtIndexPath:中的 delegate。
  4. 配置 TableViewController 以支持重新排序和单元格操作。
  5. 在您的 SNLInteractionCell 子类中配置单元格。

3. + 4. 配置 TableViewController

#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SNLExampleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // set cells delegate to connect swipe action method
    cell.delegate = self;

    // initialize colors, images and toolbar in your SNLInteractionCell subclass
    // see SNLExampleTableViewCell.m

    // configure example content
    [cell.label setText:[self.itemList objectAtIndex:indexPath.row]];

    return cell;
}



#pragma mark - SNLInteractionTableView delegate - Reorder

- (void)startedReorderAtIndexPath:(NSIndexPath *)indexPath {
    // additional setup when reordering starts
    NSLog(@"Reordering started");
}

// Update your data source when a cell is draged to a new position. This method is called every time 2 cells switch positions.
- (void)moveRowFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    // update DataSource when cells are switched
    NSLog(@"Switched Cells");

    // Reorder example:
    id object = [self.itemList objectAtIndex:fromIndexPath.row];
    [self.itemList removeObjectAtIndex:fromIndexPath.row];
    [self.itemList insertObject:object atIndex:toIndexPath.row];
}

- (void)finishedReorderAtIndexPath:(NSIndexPath *)indexPath; {
    // additional cleanup when reordering ended
    NSLog(@"Reordering ended");
}



#pragma mark - SNLInteractionCell delegate

- (void)swipeAction:(SNLSwipeSide)swipeSide onCell:(SNLExampleTableViewCell *)cell {
    // implement actions on successfull swipe gesture
    if (swipeSide == SNLSwipeSideLeft) {
        NSLog(@"Left on '%@'", cell.label.text);
    }
    else if (swipeSide == SNLSwipeSideRight) {
        NSLog(@"Right on '%@'", cell.label.text);
        [self performSegueWithIdentifier:@"detail" sender:self];
    }
}

- (void)buttonActionWithTag:(NSInteger)tag onCell:(SNLExampleTableViewCell *)cell {
    if (tag == 1) {
        NSLog(@"First Button on '%@'", cell.label.text);
    }
    else if (tag == 2) {
        NSLog(@"Second Button on '%@'", cell.label.text);
    }
}

5. 单元格初始化

    /*
    // to override default/storyboard colors use:
    self.colorBackground = [UIColor grayColor];
    self.colorContainer = [UIColor whiteColor];
    self.colorSelected = [UIColor greenColor];
    self.colorToolbarBarTint = [UIColor blueColor];
    self.colorToolbarTint = [UIColor greenColor];
    self.colorIndicator = [UIColor redColor];
    self.colorIndicatorSuccess = [UIColor greenColor];
    self.colorCustomSeparatorTop = [UIColor whiteColor];
    self.colorCustomSeparatorBottom = [UIColor grayColor];
    */


    // configure left and right swipe indicator
    [self configureSwipeOn:SNLSwipeSideLeft
       withCancelAnimation:SNLSwipeAnimationDefault
       andSuccessAnimation:SNLSwipeAnimationSlideBack
                  andImage:[UIImage imageNamed:@"indicator"]
         andImageOnSuccess:[UIImage imageNamed:@"indicator_success"]];

    [self configureSwipeOn:SNLSwipeSideRight
       withCancelAnimation:SNLSwipeAnimationDefault
       andSuccessAnimation:SNLSwipeAnimationSlideOut
                  andImage:[UIImage imageNamed:@"indicator"]
         andImageOnSuccess:[UIImage imageNamed:@"indicator_success"]];


    // setup toolbar, if toolbar is enabled (default)
    UIBarButtonItem *buttonA = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(buttonPressed:)];
    buttonA.tag = 1;

    UIBarButtonItem *buttonB = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(buttonPressed:)];
    buttonB.tag = 2;

    UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [self setToolbarButtons: [NSArray arrayWithObjects:flexibleItem, buttonA, flexibleItem, buttonB, flexibleItem, nil]];

作者:

Simon Nickel, [email protected]

许可证:

SNLInteractionTableView 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。