gpt4 book ai didi

arrays - 将 JSON 中的 URL 分配给 UICollectionView 单元格中的按钮

转载 作者:行者123 更新时间:2023-11-28 15:04:46 24 4
gpt4 key购买 nike

在 collectionView 的 cellForItemAt 中,我试图分配一个 URL 以从 JSON 打开一个按钮。我无法使用 UIApplication.shared.open,因为我在扩展程序中。

这是我试过的,我应该怎么做?

我有一个函数可以打开静态 URL,但无法在 Collection View 中分配值。

功能:

@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)

编辑:这是我使用 socket 和操作时的错误输出:

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 向 ViewController 本身提供 shareButton 操作。

@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)
}

关于arrays - 将 JSON 中的 URL 分配给 UICollectionView 单元格中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48642436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com