【问题标题】:Reorder tableview cell with a custom button使用自定义按钮重新排序 tableview 单元格
【发布时间】:2015-08-04 06:24:25
【问题描述】:

我正在开发一个应用程序,我有一个 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 上实现音乐队列列表之类的东西。

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

提前致谢:)

【问题讨论】:

  • 看看这些现有的线程,也许他们能帮上忙? stackoverflow.com/questions/5109250/…stackoverflow.com/questions/2313252/…
  • @GurtejSingh 谢谢,我正在为我的表格视图使用自定义单元格来管理滑动以一起删除和重新排序,但是重新排序按钮的位置与删除冲突,所以我需要更改重新排序按钮
  • 您能否发布一些屏幕截图,说明您所面临的问题到底是什么?谢谢
  • @GurtejSingh 谢谢,我已经添加了屏幕截图,如果你看到,重新排序按钮和删除按钮已折叠!
  • 感谢您添加屏幕截图。您能否显示如何将重新订购按钮添加到单元格中的代码?一旦您向我展示添加按钮的代码,我将能够发表评论。谢谢!

标签: ios objective-c uitableview


【解决方案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];
}

【讨论】:

  • 谢谢。这很棒,但仍然与我想要的不同,我希望重新排序按钮随着单元格滚动而移动。您只是在更改按钮位置,我想更改重新排序按钮视图。
  • 它将随着单元格滚动而移动,因为我们将其设置为UITableViewCell 内容视图的子视图。您可以完全自定义重新排序按钮。我将编辑我的答案以提供更多信息。
  • 仅供参考,我使用以下代码来管理我的表格视图单元格的滑动删除,我的单元格是这样自定义的:github.com/adamraudonis/UITableViewCell-Swipe-for-Options
  • 我会尝试重新创建您的案例并为您提供解决方案。顺便说一句,您是如何在显示重新排序控制时启用滑动删除的?
  • 正如我与您分享的那样,我使用此链接 - github.com/adamraudonis/UITableViewCell-Swipe-for-Options - 创建滑动删除,并使用 UITableView 代表移动我共享的行在我的问题中。
【解决方案2】:

我使用代码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
  }
}

【讨论】:

  • UITableViewCellEditingStyleDelete 不显示删除按钮吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 2013-02-13
  • 2016-05-11
相关资源
最近更新 更多