在iOS中的UItableview中按行到顶部

时间:2013-11-11 09:57:48

标签: ios objective-c cocoa-touch uitableview

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

3 个答案:

答案 0 :(得分:3)

我假设你的-cellForRowAtIndexPath从一个名为arrObjects的数组中加载单元格内容:

  1. NSMutableArray对象
  2. 有很多NSString个对象
  3. 类似的东西:

    - (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/

答案 1 :(得分:2)

你试过吗

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

如果你愿意,你可以使用动画..

答案 2 :(得分:1)

你可以这样轻松地做到这一点:

[self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];

或:

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

或:

[self.tableView scrollRectToVisible:CGRectMake(0.f, 0.f, CGRectGetWidth(self.tableView.frame), CGRectGetHeight(self.tableView.frame)) animated:YES];

修改
对不起,我误解了你的意思。 你应该这样做:

[tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

如果您想在移动单元格后立即滚动到顶部,可以尝试上面的代码。

修改

    // A more detailed description
- (void)buttonHandle:(UIButton *)sender {
    // Assuming button is added directly on the cell.
    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    [tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}