回答[如果您没有使用任何配件(详细信息披露等)]
(0) 我假设您已设置好表格等。
(1) 仅当您的表格单元格始终可拖动时,此解决方案才有效。将其添加到 viewDidLoad 中的 Table View Controller .m 文件中。
- (void)viewDidLoad
{
[super viewDidLoad];
[self setEditing:YES];
}
(2) 为了使您可以重新排序单元格,请添加cell.showsReorderControl = YES;
到您的 tableView:cellForRowAtIndexPath:。
(3) 确保您具有 tableView:canMoveRowAtIndexPath: 和 tableView:moveRowAtIndexPath:toIndexPath: 方法。
- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
(4) 因为您想要左侧的重新排序控件,所以我们必须删除通常在那里的删除圆圈,并且 tableView:editingStyleForRowAtIndexPath: 方法可以做到这一点。
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
(5) 最后一步,神奇的地方 - 添加 tableView:willDisplayCell:forRowAtIndexPath: 方法,搜索单元格的子视图,将其范围缩小到私有 UITableViewCellReorderControl,最后覆盖它。
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
for(UIView* view in cell.subviews)
{
if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
{
// Creates a new subview the size of the entire cell
UIView *movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
// Adds the reorder control view to our new subview
[movedReorderControl addSubview:view];
// Adds our new subview to the cell
[cell addSubview:movedReorderControl];
// CGStuff to move it to the left
CGSize moveLeft = CGSizeMake(movedReorderControl.frame.size.width - view.frame.size.width, movedReorderControl.frame.size.height - view.frame.size.height);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, -moveLeft.width, -moveLeft.height);
// Performs the transform
[movedReorderControl setTransform:transform];
}
}
}
我花了近十个小时试图找出配件的解决方案,但我无法做到。我首先需要更多的经验和知识。简单地对公开按钮上的重新排序控件执行相同的操作是行不通的。所以我希望这暂时有效;如果我将来弄清楚了,我一定会更新它。
嗨,Hoang Van Ha,这是我的第一个答案,我一个月前才开始学习 Objective-C,所以我还是一个菜鸟。
我一直在尝试用我的应用程序做同样的事情,并且已经花了几个小时寻找如何做到这一点。苹果的文档在这方面并没有多大帮助。当人们简单地链接到文档时,我发现这非常令人沮丧。我想对他们大喊大叫。希望我的回答对你有帮助。
很多荣誉b2Cloud http://b2cloud.com.au/about因为我使用了他们的一些代码本文 http://b2cloud.com.au/how-to-guides/reordering-a-uitableviewcell-from-any-touch-point并根据我的答案进行了调整。