UITableViewCell拖動(dòng)排序

UITableViewCell拖動(dòng)排序功能系統(tǒng)本身就有的,不過系統(tǒng)的只能長按一個(gè)按鈕才能拖動(dòng),如何實(shí)現(xiàn)整行可以長按拖動(dòng)呢?
思路簡單,將系統(tǒng)的長按view改變大小鋪滿cell就行。

1.找到cell的拖動(dòng)view。
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    if (_moveView) return;
    NSString *type = @"UITableViewCellReorderControl";
    for (UIView * view in self.subviews) {
        if ([NSStringFromClass([view class]) isEqualToString:type] || [NSStringFromClass([view class]) rangeOfString: @"Reorder"].location != NSNotFound) {
            view.frame = CGRectMake(0, 0, S_Width, 60);
            for (UIView *imageView in view.subviews) {
                //隱藏系統(tǒng)的拖動(dòng)icon,如果需要的隱藏的話
                imageView.hidden = YES;
            }
            _moveView = view;
            [self loadUIWithView:_moveView];
        } else {
            //隱藏不必要UI元素,否則會影響其他控件交互區(qū)域
            view.hidden = YES;
        }
    }
}

這里需要注意的是這個(gè)視圖會在setEditing方法調(diào)用之后才會添加到cell,在cell初始化時(shí)還沒有的,而setEditing方法需要開啟tableView.editing = YES。

2.以獲取到的_moveView作為backView添加控件就行(注意控件要添加到_moveView上)。

在cell重用過程中會出現(xiàn)控件偏移的情況,可以這樣:

- (void)layoutSubviews {
    [super layoutSubviews];
    _moveView.frame = CGRectMake(0, 0, S_Width, 60);
}

cell編輯模式會有刪除icon等,可以這樣:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容