在UICollectionView Cell中将JSON中的URL分配给Button

时间:2018-02-06 11:51:19

标签: 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 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

1 个答案:

答案 0 :(得分:1)

你可以试试这个。直接从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)
}