2

私はuitableviewを持っていて、各行に1つのボタン「TOP」があります。ユーザーがこのボタンをクリックすると、この行が Uitableview (アニメーション付き) で一番上にプッシュされます。そのために再利用できますBVReorderTableViewか?どうやってやるの?どうもありがとう

4

3 に答える 3

3

私が-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/
于 2013-11-11T11:32:16.827 に答える
2

やってみました

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

必要に応じてアニメーションを使用できます..

于 2013-11-11T10:09:03.523 に答える
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]];

セルを移動した直後にtableViewを一番上までスクロールしたい場合は、上記のコードを試すことができます。

編集

    // 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]];
}
于 2013-11-11T11:00:33.963 に答える