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