gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:20 25 4
gpt4 key购买 nike

我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com