Arrays 将URL从JSON分配到UICollectionView单元格中的按钮
在collectionView的cellForItemAt中,我试图分配一个URL以从JSON打开一个按钮。 我无法使用UIApplication.shared.open,因为我在扩展中 这就是我尝试过的,我应该怎么做 我有一个函数可以打开一个静态URL,但不能在集合视图中赋值 功能:Arrays 将URL从JSON分配到UICollectionView单元格中的按钮,arrays,swift,uicollectionview,Arrays,Swift,Uicollectionview,在collectionView的cellForItemAt中,我试图分配一个URL以从JSON打开一个按钮。 我无法使用UIApplication.shared.open,因为我在扩展中 这就是我尝试过的,我应该怎么做 我有一个函数可以打开一个静态URL,但不能在集合视图中赋值 功能: @objc func openUrl(url: URL?) { let selector = sel_registerName("openURL:") var responder = self as UIResp
@objc func openUrl(url: URL?) {
let selector = sel_registerName("openURL:")
var responder = self as UIResponder?
while let r = responder, !r.responds(to: selector) {
responder = r.next
}
_ = responder?.perform(selector, with: url)
}
然后在collectionView中,我尝试了以下方法:
let articleURL = self.articles[indexPath.row].url
let url = URL(string: articleURL!)
cell.shareButton.addTarget(self, action: #selector(openUrl(url: URL(string: openUrl(url: url))), for: .touchUpInside)
编辑:
以下是我使用插座时的错误输出和如下操作:
cell.shareButton.addTarget(self, action: #selector(URLButtonAcn), for: .touchUpInside)
输出:
gifSearchKeyboard[75322:4394018] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[gifSearchKeyboard.gifCollectionViewCell shareButtonAction:]: unrecognized selector sent to instance 0x7f941f4378a0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010752f12b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010656ff41 objc_exception_throw + 48
2 CoreFoundation 0x00000001075b0024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 UIKit 0x00000001086c6f51 -[UIResponder doesNotRecognizeSelector:] + 295
4 CoreFoundation 0x00000001074b1f78 ___forwarding___ + 1432
5 CoreFoundation 0x00000001074b1958 _CF_forwarding_prep_0 + 120
6 UIKit 0x0000000108494972 -[UIApplication sendAction:to:from:forEvent:] + 83
7 UIKit 0x0000000108613c3c -[UIControl sendAction:to:forEvent:] + 67
8 UIKit 0x0000000108613f59 -[UIControl _sendActionsForEvents:withEvent:] + 450
9 UIKit 0x0000000108612e86 -[UIControl touchesEnded:withEvent:] + 618
10 UIKit 0x0000000108a84bad _UIGestureEnvironmentSortAndSendDelayedTouches + 5560
11 UIKit 0x0000000108a7ea4d _UIGestureEnvironmentUpdate + 1506
12 UIKit 0x0000000108a7e41f -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 484
13 UIKit 0x0000000108a7d4cb -[UIGestureEnvironment _updateGesturesForEvent:window:] + 288
14 UIKit 0x000000010850bf14 -[UIWindow sendEvent:] + 4102
15 UIKit 0x00000001084af365 -[UIApplication sendEvent:] + 352
16 UIKit 0x0000000108dfba1d __dispatchPreprocessedEventFromEventQueue + 2809
17 UIKit 0x0000000108dfe672 __handleEventQueueInternal + 5957
18 CoreFoundation 0x00000001074d2101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19 CoreFoundation 0x0000000107571f71 __CFRunLoopDoSource0 + 81
20 CoreFoundation 0x00000001074b6a19 __CFRunLoopDoSources0 + 185
21 CoreFoundation 0x00000001074b5fff __CFRunLoopRun + 1279
22 CoreFoundation 0x00000001074b5889 CFRunLoopRunSpecific + 409
23 GraphicsServices 0x000000010f7fe9c6 GSEventRunModal + 62
24 UIKit 0x00000001084935d6 UIApplicationMain + 159
25 libxpc.dylib 0x000000010b6d0b50 _xpc_objc_main + 491
26 libxpc.dylib 0x000000010b6d2ff0 xpc_main + 143
27 Foundation 0x0000000105fbc091 +[NSXPCListener serviceListener] + 0
28 PlugInKit 0x0000000111dc71ef -[PKService run] + 709
29 PlugInKit 0x0000000111dc6df6 +[PKService main] + 55
30 PlugInKit 0x0000000111dc7213 +[PKService _defaultRun:arguments:] + 17
31 Foundation 0x000000010606d31a NSExtensionMain + 51
32 libdyld.dylib 0x000000010b378d81 start + 1
33 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
你可以试试这个。直接从UICollectionViewCell将shareButton操作赋予ViewController本身
@IBOutlet weak var collxnVw: UICollectionView! // OUTLET CONNECTION
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! yourCollectionViewCell
//let articleURL = self.articles[indexPath.row].url
//let url = URL(string: articleURL!)
//cell.shareButton.addTarget(self, action: #selector(openUrl(url: URL(string: openUrl(url: url))), for: .touchUpInside)
//comment the above lines. Use outlet connection for button action.
return cell
}
@IBAction func URLButtonAcn(_ sender: UIButton) {
let cellPosition = sender.convert(CGPoint.zero, to: collxnVw)
let indPath : IndexPath = collxnVw.indexPathForItem(at: cellPosition)!
//let specificCell = collxnVw.cellForItem(at: indPath) as! SOCollectionViewCell
//HERE U CAN GET SPECIFIC CELL
let articleURL = self.articles[indPath.row].url
let url = URL(string: articleURL!)
let selector = sel_registerName("openURL:")
var responder = self as UIResponder?
while let r = responder, !r.responds(to: selector) {
responder = r.next
}
_ = responder?.perform(selector, with: url)
print("Button's IndPath is ", indPath)
}
我已从UICollectionViewCell更新了按钮单击操作的答案。不要使用该选项,cell.shareButton.addTarget。只需将UIButton操作拖动到此ViewController本身,就像我的回答一样。把那条线移开。好的,谢谢。因此,button元素位于具有自己类的Nib中,并且您提供的代码位于视图控制器中,因此,请确保我理解,我将把Nib中的按钮连接到视图控制器中的操作,对吗?尝试使用故事板。Viewcontroller->UICollectionView->UICollectionViewCell->UIButton。对不起,我必须从nib执行此项目的collectionView。谢谢你的建议!谢谢,当我添加这段代码时,在我将collxnVw更改为collectionView后,我得到了以下错误:无法使用“CGPoint,to:u”类型的参数列表调用“convert”,但当我将其更改为collectionView名称self.key.colImages时,错误消失,但单击时会崩溃。我已更新了相应的内容。你能更新你的代码吗?啊,是的,会给它一个tryself.key.colImages这是什么?UICollectionView销售点在哪里?检查我的答案。根据您的回答进行更新,但可能我没有正确使用它?