使用自定义按钮重新排序tableview单元格

时间:2015-08-04 06:24:25

标签: ios objective-c uitableview

我正在开发一个应用程序,我有一个UITableView,我使用以下代码来管理重新排序:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
if(proposedDestinationIndexPath.section==0 && proposedDestinationIndexPath!=sourceIndexPath){

    return proposedDestinationIndexPath;
}
else{
    return sourceIndexPath;
   }
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ 
   return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
  //move rows
}

它工作正常,但我想更改重新排序按钮位置并在iOS 8.4上实现类似音乐队列列表。

我想将重新排序按钮更改为滚动视图上的单元格内容,并随单元格移动。 截图: enter image description here

提前致谢:)

2 个答案:

答案 0 :(得分:1)

根据this教程,您可以将UITableViewCellReorderControl设置为UITableViewCellContentView的一部分,以便它随着单元格的内容移动:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *reorderControl = [cell huntedSubviewWithClassName:@"UITableViewCellReorderControl"];
    UIView *contentView = [cell huntedSubviewWithClassName:@"UITableViewCellContentView"];

    UIView *resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(reorderControl.frame), CGRectGetMaxY(reorderControl.frame))];
    [resizedGripView addSubview:reorderControl];
    [contentView addSubview:resizedGripView];

    //  Original transform
    CGAffineTransform transform = CGAffineTransformIdentity;

    //  Move reorderControl to the left with an arbitrary value
    transform = CGAffineTransformTranslate(transform, -100, 0);

    [resizedGripView setTransform:transform];
}

答案 1 :(得分:0)

我使用代码here解决了我的问题以进行重新排序,然后我使用UITableView委托进行滑动删除。

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //Remove selected cell from tableview and you data source
  }
}