如何防止 UITableViewCell 移动 (Swift 5)
这实际上是一个由两部分组成的问题。首先看一下代码:
//canEditRowAt
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if tableView.tag == 1000{
return indexPath.row == 0 ? false : true
}
return true
}
//canMoveRowAt
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
if tableView.tag == 1000{
return indexPath.row == 0 ? false : true
}
return true
}
所以从这里我希望它会阻止索引 0 处的行让其他单元格移动到它,但是不会...正如您所看到的:
我明明想阻止却做不到似乎找到任何文档来解决该问题。
我正在努力解决的另一件事;如果您查看记录,您可以看到,一旦我将单元格移动到任何位置,单元格后面就会出现一个黑条。我也想避免这种情况发生,并尝试了几种方法,但没有任何效果。
任何帮助表示赞赏,谢谢!
This is actually a two part question. First take a look at the code:
//canEditRowAt
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if tableView.tag == 1000{
return indexPath.row == 0 ? false : true
}
return true
}
//canMoveRowAt
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
if tableView.tag == 1000{
return indexPath.row == 0 ? false : true
}
return true
}
So from this I would expect that it would prevent the row at index 0 from having other cells move to it, but no... as you can see:
I obviously want to prevent it, but can't seem to find any documentation to solve the issue.
The other bit I'm struggling with; if you look at the recording you can see that once I move a cell into any location, a black bar appears behind the cell. I would like to avoid that from happening as well, and have tried several things but nothing has worked.
Any help is appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要回答第一个问题,如果您查看
tableView(_:canMoveRowAt:)
文档:这主要讨论显示的
重新排序控件
,而不是具体说明它永远不能移动。因此,如果您查看 UI,会发现indexPath.row == 0
时未显示重新排序控件
,我可以建议 2 个替代方案:
1。反转移动操作
2.使用标题视图
这样您就不必担心指定哪些单元格可以移动、哪些单元格不能移动的任何逻辑,因为您可以在标题视图中拥有不可移动的数据:
我推荐第二个选项,因为它具有更好的用户体验,并且似乎是解决此问题的正确方法。
要回答第二个问题,在您的 cellForRowAt indexPath 或自定义单元格实现中,您可能将单元格的背景视图或 contentView 设置为黑色。
尝试设置其中之一或两者:
这不应该给你一个黑色背景
To answer the first question, if you look at the
tableView(_:canMoveRowAt:)
documentation :This mainly talks about the
reordering control
being shown rather than specifically saying it can never be moved. So if you look at your UI, thereordering control
is not showing forindexPath.row == 0
I can suggest 2 alternatives:
1. Reverse the move action
2. User a header view
This way you don't have to worry about specify any logic of which cells can move and which cannot as you can have the non movable data in a header view:
I recommend the second option as it has the better user experience and seems to be the right way of approaching this problem.
To answer your second question, in your
cellForRowAt indexPath
or custom cell implementation, you probably set the background view of the cell or thecontentView
to black.Try setting one of these or both:
This should not give you a black background
实现
tableView(_:targetIndexPathForMoveFromRowAt:toProposedIndexPath:)
并确保永远不会返回第 0 行(当proposedDestinationIndexPath
为第 0 行时返回第 1 行)。Implement
tableView(_:targetIndexPathForMoveFromRowAt:toProposedIndexPath:)
and ensure row 0 is never returned (return row 1 whenproposedDestinationIndexPath
is row 0).