關於tableView點擊cell中按鈕進入排序模式(cell行高統一可長按排序)

RTDragCellTableView:  https://github.com/RustedBigB/RTDragCellTableViewgit

首先,爲三方的tableview類添加了一個isMove的BOOL屬性,用於標記是否處於排序狀態。github

在三方tableview類裏的長按的方法裏判斷isMove的值。若是不是排序狀態直接return。數組

if (!self.isMove) {ui

        return;.net

    }代理

爲自定義的cell聲明一個代理,在點擊cell中的按鈕時,使用代理改變tableview的isMove的值,使tableview進入排序狀態。orm

- (void)myTableViewCell:(MyTableViewCell *)cell funcBtnDidClick:(UIButton *)func {對象

#pragma mark -- 改變數據源數組;排序

     NSMutableArray *arrayM = [NSMutableArray array];字符串

    //遍歷數據源數組;

    for (NSInteger i = 0; i < self.data.count; i ++) {

        {//在這裏是由於須要在排序模式下每一行的文字都須要能單獨排序,

//因此在進入排序模式的時候對數據源作了一些處理。

        RTModel *model = self.data[i];

        if (model.title.length) {//判斷數組對應位置的對象是否是文本屬性;

            //對應位置是字符串須要進行處理;

            NSString *strA = model.title;

            while ([strA rangeOfString:@"\n"].length) {//存在\n;

                NSRange range= [strA rangeOfString:@"\n"];

                NSString *subStr = [strA substringToIndex:range.location];

                //加入數組;

                RTModel *newModel = [[RTModel alloc]init];

                newModel.title = subStr;

                [arrayM addObject:newModel];

                strA = [strA substringFromIndex:range.length+range.location];

            }

            //最後剩餘的字符串不存在換行,因此須要單獨添加。

            RTModel *lastModel = [[RTModel alloc]init];

            lastModel.title = strA;

            [arrayM addObject:lastModel];

}            

        }else {//不是文本屬性直接加入的可變數組

            [arrayM addObject:model];

        }

    }

    //改變數據數組;

     self.data = arrayM.copy;

    self.isMove = YES;

    [_tableView reloadData];

    _tableView.isMove = YES;

}

在控制器的代理方法中刷新tableview,使cell的高度根據是否處於排序狀態來返回固定值或者其餘。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    RTModel *model = self.data[indexPath.row];

    UIFont *font = [UIFont systemFontOfSize:15];

    NSDictionary *attribute = @{NSFontAttributeName: font};

    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 0);

    CGSize retSize = [model.title boundingRectWithSize:size

                                             options:\

                      NSStringDrawingTruncatesLastVisibleLine |

                      NSStringDrawingUsesLineFragmentOrigin |

                      NSStringDrawingUsesFontLeading

                                          attributes:attribute

                                             context:nil].size;

    CGFloat h = retSize.height + 40;

//這裏的高度計算純粹爲了演示,請按本身的需求返回。

    return self.isMove ? 40 : h ;

}

同理,在tableview的拖拽完成的代理方法裏面將isMove的值改成no,繼續刷新tableview。

- (void)cellDidEndMovingInTableView:(RTDragCellTableView *)tableView {

{

/*

在這裏,若是不對數據源作處理排序完成後每一行文字就是一個cell,都會附帶排序按鈕,感受上不是很好。

因此,在這裏將相鄰的cell模型若是是文字的內容就進行了合併,並在中間添加了「\n」使相鄰的文本的cell成爲一個cell。

*/

#pragma mark -- 移動完成後合併相鄰的文字輸入;

    NSMutableArray *arrayM = [NSMutableArray array];

    for (NSInteger i = 0; i < self.data.count; ) {

        RTModel *model = self.data[i];

        if (model.title.length) {

            NSString *strNew = model.title;

            while (++i < self.data.count&&[self.data[i] title].length) {

                RTModel *nextModel = self.data[i];

                strNew = [NSString stringWithFormat:@"%@\n%@",strNew,nextModel.title];

            }

            RTModel *newModel = [[RTModel alloc]init];

            newModel.title = strNew;

            [arrayM addObject:newModel];

            

        }else {//不是文本輸入直接加入到新的可變數組;

            [arrayM addObject:model];

            i ++;

        }

    }

    self.data = arrayM.copy;

}

    self.isMove = NO;

    [_tableView reloadData];

    _tableView.isMove = NO;

}

 

http://git.oschina.net/ruiruiheshui/RTDragCellTableView_test

相關文章
相關標籤/搜索