BVReorderTableView 1.0.0

BVReorderTableView 1.0.0

测试已测试
语言语言 Objective-CObjective C
许可证 MIT
发布日期上次发布2014年12月

Ben Vogelzang 维护。




  • Ben Vogelzang

UITableView 的简单长按拖拽重排序

Example

示例用法

  1. BVReorderTableView.hBVReorderTableView.m 文件复制到您的项目中

  2. 在您的 storyboard 文件的 identity 检查器中,将您的UITableView类更改为 BVReorderTableView。可选地,创建自己的 BVReorderTableView。

  3. 在您的视图控制器中实现代理方法,并更新您的 tableView:cellForRowAtIndexPath: 方法以处理空行。

// This method is called when the long press gesture is triggered starting the re-ording process. 
// You insert a blank row object into your data source and return the object you want to save for 
// later. This method is only called once.
- (id)saveObjectAndInsertBlankRowAtIndexPath:(NSIndexPath *)indexPath {
    id object = [_objects objectAtIndex:indexPath.row];
    // Your dummy object can be something entirely different. It doesn't
    // have to be a string.
    [_objects replaceObjectAtIndex:indexPath.row withObject:@"DUMMY"];
    return object;
}

// This method is called when the selected row is dragged to a new position. You simply update your
// data source to reflect that the rows have switched places. This can be called multiple times
// during the reordering process.
- (void)moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    id object = [_objects objectAtIndex:fromIndexPath.row];
    [_objects removeObjectAtIndex:fromIndexPath.row];
    [_objects insertObject:object atIndex:toIndexPath.row];
}


// This method is called when the selected row is released to its new position. The object is the same
// object you returned in saveObjectAndInsertBlankRowAtIndexPath:. Simply update the data source so the
// object is in its new position. You should do any saving/cleanup here.
- (void)finishReorderingWithObject:(id)object atIndexPath:(NSIndexPath *)indexPath; {
    [_objects replaceObjectAtIndex:indexPath.row withObject:object];
    // do any additional cleanup here
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // You will have to manually configure what the 'empty' row looks like in this
    // method. Your dummy object can be something entirely different. It doesn't
    // have to be a string.
    if ([[_objects objectAtIndex:indexPath.row] isKindOfClass:[NSString class]] &&
        [[_objects objectAtIndex:indexPath.row] isEqualToString:@"DUMMY"]) {
        cell.textLabel.text = @"";
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else {
        NSDate *object = _objects[indexPath.row];
        cell.textLabel.text = [object description];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

查看与这些文件一起提供的 ReorderTest 示例项目,以获取工作示例。

要求

BVReorderTableView 需要 iOS 5.0 及以上版本。

ARC

BVReorderTableView 自 1.0 版本起使用 ARC。