以前,我尝试用 UITableView 中的长按拖动来替换标准的 Apple 重新排序控件(从右侧的 handle 拖动单元格)。但是,由于某种原因,我无法通过长按拖动将单元格移动到已经没有单元格的部分。现在我正在尝试实现一个功能,用户可以在 UICollectionViewController 而不是 UITableView 的 2 个部分之间拖动单元格。我实现了长按拖动功能,但由于某种原因我遇到了同样的问题。我将如何向这些部分添加一个虚拟单元格,以便它们永远不会为空,或者是否有更好的方法来解决这个问题?是否还有一种无需长按即可拖动单元格的方法?
这些是我添加到我的 UICollectionViewController 类中以启用长按拖动的功能:
override func viewDidLoad() {
super.viewDidLoad()
let longPressGesture = UILongPressGestureRecognizer(target: self, action: "handleLongGesture:")
self.collectionView!.addGestureRecognizer(longPressGesture)
}
func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch(gesture.state) {
case UIGestureRecognizerState.Began:
guard let selectedIndexPath = self.collectionView!.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else {
break
}
collectionView!.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
case UIGestureRecognizerState.Changed:
collectionView!.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
case UIGestureRecognizerState.Ended:
collectionView!.endInteractiveMovement()
default:
collectionView!.cancelInteractiveMovement()
}
}
override func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let fromRow = sourceIndexPath.row
let toRow = destinationIndexPath.row
let fromSection = sourceIndexPath.section
let toSection = destinationIndexPath.section
var item: Item
if fromSection == 0 {
item = section1Items[fromRow]
section1Items.removeAtIndex(fromRow)
} else {
item = section2Items[sourceIndexPath.row]
section2Items.removeAtIndex(fromRow)
}
if toSection == 0 {
section1Items.insert(score, atIndex: toRow)
} else {
section2Items.insert(score, atIndex: toRow)
}
}
override func collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
谢谢
最佳答案
要首先回答问题的第二部分,请使用 UIPanGestureRecogniser 而不是 UILongPressRecogniser。
对于空白部分,您可以向空白部分添加一个不可见的虚拟单元格。在 Storyboard中创建一个没有 subview 的原型(prototype)单元格,并确保它具有与 Collection View 相同的背景颜色。
如果该部分为空,您需要安排显示此单元格。但是您还需要在只有 1 个单元格的部分开始时添加一个虚拟单元格,否则该部分将在移动过程中折叠并且用户无法将单元格移回该部分从那里开始。
在手势处理程序中,开始移动时添加一个临时单元格。如果尚未移除单元格,则在拖动完成时也将其移除(如果单元格实际上并未移动,则不会调用委托(delegate) moveItemAtIndexPath 方法):
var temporaryDummyCellPath:NSIndexPath?
func handlePanGesture(gesture: UIPanGestureRecognizer) {
switch(gesture.state) {
case UIGestureRecognizerState.Began:
guard let selectedIndexPath = self.collectionView.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else {
break
}
if model.numberOfPagesInSection(selectedIndexPath.section) == 1 {
// temporarily add a dummy cell to this section
temporaryDummyCellPath = NSIndexPath(forRow: 1, inSection: selectedIndexPath.section)
collectionView.insertItemsAtIndexPaths([temporaryDummyCellPath!])
}
collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
case UIGestureRecognizerState.Changed:
collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
case UIGestureRecognizerState.Ended:
collectionView.endInteractiveMovement()
// remove dummy path if not already removed
if let dummyPath = self.temporaryDummyCellPath {
temporaryDummyCellPath = nil
collectionView.deleteItemsAtIndexPaths([dummyPath])
}
default:
collectionView.cancelInteractiveMovement()
// remove dummy path if not already removed
if let dummyPath = temporaryDummyCellPath {
temporaryDummyCellPath = nil
collectionView.deleteItemsAtIndexPaths([dummyPath])
}
}
}
在collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int)始终返回比模型中项目数多 1 的值,或者如果添加了临时虚拟单元格,则返回一个额外的单元格。
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// special case: about to move a cell out of a section with only 1 item
// make sure to leave a dummy cell
if section == temporaryDummyCellPath?.section {
return 2
}
// always keep one item in each section for the dummy cell
return max(model.numberOfPagesInSection(section), 1)
}
在collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)如果该行等于该部分模型中的项目数,则分配虚拟单元格。
禁止选择 collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) 中的单元格通过为虚拟单元格返回 false 而为其他单元格返回 true。
同样禁止移动 collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) 中的单元格
确保目标移动路径仅限于模型中的行:
func collectionView(collectionView: UICollectionView, targetIndexPathForMoveFromItemAtIndexPath originalIndexPath: NSIndexPath, toProposedIndexPath proposedIndexPath: NSIndexPath) -> NSIndexPath
{
let proposedSection = proposedIndexPath.section
if model.numberOfPagesInSection(proposedSection) == 0 {
return NSIndexPath(forRow: 0, inSection: proposedSection)
} else {
return proposedIndexPath
}
}
现在您需要处理最后一步。更新您的模型,然后根据需要添加或删除虚拟单元格:
func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
{
// move the page in the model
model.movePage(sourceIndexPath.section, fromPage: sourceIndexPath.row, toSection: destinationIndexPath.section, toPage: destinationIndexPath.row)
collectionView.performBatchUpdates({
// if original section is left with no pages, add a dummy cell or keep already added dummy cell
if self.model.numberOfPagesInSection(sourceIndexPath.section) == 0 {
if self.temporaryDummyCellPath == nil {
let dummyPath = NSIndexPath(forRow: 0, inSection: sourceIndexPath.section)
collectionView.insertItemsAtIndexPaths([dummyPath])
} else {
// just keep the temporary dummy we already created
self.temporaryDummyCellPath = nil
}
}
// if new section previously had no pages remove the dummy cell
if self.model.numberOfPagesInSection(destinationIndexPath.section) == 1 {
let dummyPath = NSIndexPath(forRow: 0, inSection: destinationIndexPath.section)
collectionView.deleteItemsAtIndexPaths([dummyPath])
}
}, completion: nil)
}
最后确保虚拟单元格没有辅助功能项,以便在打开画外音时跳过它。
关于ios - Swift:无法长按拖动单元格到 UICollectionViewController 和 UITableView 中的空白部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35920874/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'