ios - 在 iOS 的 UItableview 中将行推到顶部

标签 ios objective-c cocoa-touch uitableview

我有 uitableview,每行有 1 个按钮“TOP”。我想当用户单击此按钮时,此行已被推到 Uitableview 的顶部(带有动画)。我可以重用 BVReorderTableView 来做到这一点吗?我怎样才能做到这一点?非常感谢

最佳答案

我假设您的 -cellForRowAtIndexPath 从数组中加载单元格内容,例如我命名为 arrObjects 的数组:

  1. 是一个NSMutableArray对象
  2. 有很多 NSString 对象

类似于:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //...
    [cell.textLabel setText:[arrObjects objectAtIndex:indexPath.row]];

    UIButton *btnUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnUp setFrame:CGRectMake(0, 0, 30, 30)];
    //[btnUp setTag:100];
    [btnUp setTitle:@"\u261D" forState:UIControlStateNormal];
    [btnUp addTarget:self
              action:@selector(moveToTop:)
    forControlEvents:UIControlEventTouchUpInside];
    [cell setAccessoryView:btnUp];

    return cell;
}

- (void)moveToTop:(UIButton *)sender
{
    UITableViewCell *cell = (UITableViewCell *)sender.superview; //iOS6 prior
    //UITableViewCell *cell = (UITableViewCell *)sender.superview.superview; //iOS7

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    //uncomment lines for safety, incase indexPath ever comes nil... prevent the crash
    //if (indexPath == nil) {
    //    return;
    //}

    //1. move and animate row to top
    [self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row
                                                           inSection:indexPath.section]
                           toIndexPath:[NSIndexPath indexPathForItem:0
                                                           inSection:indexPath.section]];


    //2. make appropriate changes to the datasource
    //so that it reflects logically and is not just an aestehtical change

    //PRE-NOTE:
    //arrObjects is an object of a category on NSMutableArray
    //with a custom instance method named -moveObjectFromIndexPath:toIndex:

    //uncomment the following line when you are ready with the category
    //[arrObjects moveObjectFromIndex:indexPath.row toIndex:0];
}

类别创建起来很简单,按照:
http://www.icab.de/blog/2009/11/15/moving-objects-within-an-nsmutablearray/


其他尝试链接:

  1. http://adeem.me/blog/2009/05/29/iphone-sdk-tutorial-add-delete-reorder-uitableview-row/
  2. http://learn-iphone-app-development.com/2011/01/31/tables-part-iii-%E2%80%94-re-ordering-moving-cells-and-swipe-to-delete/

关于ios - 在 iOS 的 UItableview 中将行推到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19903432/

相关文章:

ios - iOS 10 中新的 UserNotifications 是否仍有 ~64 个本地通知限制?

php - 在 iOS 中检索 SQL 数据

iPhone - 使用 2 个 MKMapPoint 定义 MKMapRect

iphone - 如何通过单击激活 CCMenuItem 并移动 CCSprite

iOS - 资源内容可供公众访问

ios - CellForItemAt : index path not updating variable

ios - Mapbox 自定义用户位置图片

objective-c - CocoaLumberjack 未登录 iOS 中的后台线程

objective-c - NSLocalizedString 格式

objective-c - 动态实例化对象 Objective-C