0

I have custom table view cell. And this code for swipe-deletion:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return reportingCellIndexPath != indexPath;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger result = [self.messagesModel numberOfDifferentDayDatesInMessages];
    return result;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDate *sectionDate = [self.messagesModel dayDateAtNumber:section];
    NSInteger rowsCount = [self.messagesModel numberOfMessagesWithDayDate:sectionDate];
    return rowsCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger realMessageIndex = [self.messagesModel fullMessageIndexForMessageWithDayDateNumber:indexPath.section index:indexPath.row];


    SDMessage *message = [self.messagesModel messageAtIndex:realMessageIndex];
//    SDMessage *message = [self.messagesModel messageAtIndex:indexPath.row withDayDateAtNumber:indexPath.section];
    static NSString *messageCellId = @"messageCellId";
    SDMessageCell *_messageCell = [tableView dequeueReusableCellWithIdentifier:messageCellId];
    if (_messageCell == nil) {
        _messageCell = [[SDMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageCellId];
    }
    [_messageCell rebuildWithMessage:message];
    _messageCell.delegate = self;
    if ([reportingCellIndexPath isEqual:indexPath] &&
        ![_messageCell reportViewIsShowing]) {
        [_messageCell showReportViewWithMessagesModel:self.messagesModel messageIndex:realMessageIndex];
    } else if ([_messageCell reportViewIsShowing]) {
        [_messageCell hideReportView];
    }
    return _messageCell;


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat result = [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
    return result;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSDate *sectionDate = [self.messagesModel dayDateAtNumber:section];
    if (sectionDate != nil) {
        return [self sectionTitleForSectionDate:sectionDate];
    } else {
        return @"";
    }
}


- (NSString *)sectionTitleForSectionDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDate *now = [NSDate date];

    NSDateComponents *dateComponents = [calendar components:
                                        NSYearCalendarUnit |
                                        NSMonthCalendarUnit |
                                        NSDayCalendarUnit |
                                        NSWeekdayCalendarUnit |
                                        NSWeekCalendarUnit
                                                   fromDate:date];
    NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit |
                                       NSMonthCalendarUnit |
                                       NSDayCalendarUnit |
                                       NSWeekdayCalendarUnit |
                                       NSWeekCalendarUnit
                                                  fromDate:now];

    if (dateComponents.year == nowComponents.year &&
        dateComponents.month == nowComponents.month &&
        dateComponents.week == nowComponents.week) {
        if (nowComponents.weekday - dateComponents.weekday < 2) {
            [dateFormatter setDoesRelativeDateFormatting:YES];
        } else {
            [dateFormatter setDateFormat:@"EEEE"];
        }
    }

    return [dateFormatter stringFromDate:date];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self cancelSearchField];
    NSInteger realMessageIndex = [self.messagesModel fullMessageIndexForMessageWithDayDateNumber:indexPath.section index:indexPath.row];
    if (indexPath.row < self.messagesModel.size && realMessageIndex > -1) {

        [self.messagesModel selectMessageAtIndex:realMessageIndex];

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            self.refreshBeforeShowing = NO;
            [self.navigationController pushViewController:self.detailViewController animated:YES];
        }
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSInteger realMessageIndex = [self.messagesModel fullMessageIndexForMessageWithDayDateNumber:indexPath.section index:indexPath.row];
        [SDModalLoadingIndicator showLoading];
        [self.messagesModel deleteMessageAtIndex:realMessageIndex success:^() {
            [SDModalLoadingIndicator hideLoading];

            NSString *msg = NSLocalizedString(@"Message has been deleted", nil);
            [[[[iToast makeText:msg]
               setDuration:iToastDurationNormal]
              setGravity:iToastGravityBottom] show];
            [self.tableView reloadData];
        } failureCallback:^(NSString *message, NSMutableArray *validationErrors, NSError *error) {
            [SDModalLoadingIndicator hideLoading];
            [SDFailureHandler handleConnectFailure:message withError:error];
        }];
    }
}

But almost always it don't show delete button on swipe.(Sometimes button will be showing). Do somebody know why?

I've tried to debug this thing check all views by debugger, and they have only system gesture recognizers(pan, tap, long press, edge).

Also I can say that this method "tableView:canEditRowAtIndexPath:" is called each time I swipe cell but nothing happened.(In most cases)

Vasyl Khmil
  • 2,548
  • 1
  • 20
  • 36

2 Answers2

0

You need to make sure you also have a commitEditingStyle delegate function setup.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle != UITableViewCellEditingStyleDelete)
        return;

    // perform the delete!
}

Also, remember the swipe to delete is only present when the tableView isn't being edited (in edit mode, the minus button is visible on the rows instead).

Gavin Bunney
  • 1,240
  • 1
  • 12
  • 12
0

I, just forgot that my application was using side menu. So side menu was adding pan gesture on my view for closing and opening on drag event.

And I understand if you have all necessary methods implemented and delete on swipe don't work. ALWAYS try to find some gesture or element that is catching you touch event.

Vasyl Khmil
  • 2,548
  • 1
  • 20
  • 36