iOS版。表视图单元格轻扫工作

时间:2015-02-25 22:08:18

标签: ios objective-c uitableview swipe

我有自定义表格视图单元格。这个代码用于滑动删除:

- (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];
        }];
    }
}

但几乎总是它不会在滑动时显示删除按钮。(有时按钮会显示)。有人知道为什么吗?

我试图调试这个东西通过调试器检查所有视图,它们只有系统手势识别器(平移,点击,长按,边缘)。

另外我可以说每次刷卡时都会调用此方法“tableView:canEditRowAtIndexPath:”但没有任何反应。(大多数情况下)

2 个答案:

答案 0 :(得分:0)

您需要确保您还拥有commitEditingStyle委托功能设置。

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

    // perform the delete!
}

此外,请记住,当tableView 不是正在编辑时,要删除的滑动仅 (在编辑模式下,减号按钮是在行上可见而不是。)

答案 1 :(得分:0)

我,忘了我的应用程序正在使用侧边菜单。因此,侧面菜单在我的视图上添加了平移手势,用于关闭和打开拖动事件。

我理解你是否已经实施了所有必要的方法并且在刷卡时删除了。总是试图找到一些触及你的触摸事件的手势或元素。