我有 uitableview,每行都有 1 个按钮“TOP”。我想当用户单击此按钮时,此行已在 Uitableview 中被推到顶部(带有动画)。我可以重复使用BVReorderTableView
吗?我怎样才能做到这一点?非常感谢
问问题
1802 次
3 回答
3
我假设您-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/
其他尝试链接:
于 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 回答