我有 uitableview,每行有 1 个按钮“TOP”。我想当用户单击此按钮时,此行已被推到 Uitableview 的顶部(带有动画)。我可以重用 BVReorderTableView
来做到这一点吗?我怎样才能做到这一点?非常感谢
最佳答案
我假设您的 -cellForRowAtIndexPath
从数组中加载单元格内容,例如我命名为 arrObjects
的数组:
- 是一个
NSMutableArray
对象 - 有很多
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/
其他尝试链接:
关于ios - 在 iOS 的 UItableview 中将行推到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19903432/