swift - 单击单元格按钮时应用程序崩溃

标签 swift uitableview custom-cell

我正试图让我的应用程序有一个类似于 reddit 的赞成票/反对票系统,这意味着当单击按钮以仅更改它们自己的数据时,我表中的每个单元格都必须做出不同的 react 。我创建了一个自定义单元格类并相应地设置了所有内容,但是由于某种原因,当我运行我的应用程序时,只要您点击单元格中的按钮,它就会崩溃。

这是我的代码(我试图编辑掉任何不必要的部分,但如果您想查看整个代码,请告诉我):

import UIKit
import Firebase
import FirebaseDatabase

class FindPartiesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // where we will store all the parties
    var parties = [party]()

    @IBOutlet weak var partyTable: UITableView!

    var refreshControl = UIRefreshControl()

    override func viewDidLoad() {
        super.viewDidLoad()

        partyTable.delegate = self
        partyTable.dataSource = self       
    }



    // creates the number of cells in the table
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return parties.count    

    // define all the cells
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // Make table cells the show the party
        let cell = partyTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell

       cell.nameLabel.text = parties[indexPath.row].name

       cell.LocationLabel.text = parties[indexPath.row].location

       cell.totalVotesLabel.text = (String(parties[indexPath.row].upVotes - parties[indexPath.row].downVotes))

       cell.upVoteButton.tag = indexPath.row
       cell.upVoteButton.addTarget(self, action: Selector(("upVoteAction")), for: .touchUpInside)
       cell.downVoteButton.tag = indexPath.row
       cell.downVoteButton.addTarget(self, action: Selector(("downVoteAction")), for: .touchUpInside)



        return cell
    }

    @IBAction func upVoteAction(sender: UIButton){

       print("+1")
       parties[sender.tag].upVotes = parties[sender.tag].upVotes + 1
    }

}

(更新) 我的错误信息:

2016-11-10 13:35:20.371 Lit[16625:2721786] -[Lit.FindPartiesViewController upVoteAction]: unrecognized selector sent to instance 0x7fc40b420240
2016-11-10 13:35:20.375 Lit[16625:2721786] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Lit.FindPartiesViewController upVoteAction]: unrecognized selector sent to instance 0x7fc40b420240'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001093ef34b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x0000000108e5021e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010945ef34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x0000000109374c15 ___forwarding___ + 1013
    4   CoreFoundation                      0x0000000109374798 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x0000000109814b88 -[UIApplication sendAction:to:from:forEvent:] + 83
    6   UIKit                               0x000000010999a2b2 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x000000010999a5cb -[UIControl _sendActionsForEvents:withEvent:] + 444
    8   UIKit                               0x00000001099994c7 -[UIControl touchesEnded:withEvent:] + 668
    9   UIKit                               0x0000000109d4b1dc _UIGestureEnvironmentSortAndSendDelayedTouches + 5645
    10  UIKit                               0x0000000109d45ea3 _UIGestureEnvironmentUpdate + 1472
    11  UIKit                               0x0000000109d4589b -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 521
    12  UIKit                               0x0000000109d44a7e -[UIGestureEnvironment _updateGesturesForEvent:window:] + 286
    13  UIKit                               0x00000001098837ad -[UIWindow sendEvent:] + 3989
    14  UIKit                               0x0000000109830a33 -[UIApplication sendEvent:] + 371
    15  UIKit                               0x000000010a022b6d __dispatchPreprocessedEventFromEventQueue + 3248
    16  UIKit                               0x000000010a01b817 __handleEventQueue + 4879
    17  CoreFoundation                      0x0000000109394311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    18  CoreFoundation                      0x000000010937959c __CFRunLoopDoSources0 + 556
    19  CoreFoundation                      0x0000000109378a86 __CFRunLoopRun + 918
    20  CoreFoundation                      0x0000000109378494 CFRunLoopRunSpecific + 420
    21  GraphicsServices                    0x000000010cd78a6f GSEventRunModal + 161
    22  UIKit                               0x0000000109812f34 UIApplicationMain + 159
    23  Lit                                 0x0000000106fda27f main + 111
    24  libdyld.dylib                       0x000000010ba3668d start + 1
    25  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

最佳答案

Selector("upVoteAction")

func upVoteAction(sender: UIButton)

相同,所以它根本找不到那个方法。您需要添加将前者更改为

Selector("upVoteAction:")

因为您的实际方法有一个参数。

编辑:根据@rmaddy 的评论,swift 3 方式类似于:

#selector(upVoteAction(sender:))

关于swift - 单击单元格按钮时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40534641/

相关文章:

ios - 具有 selectedSegmentIndex 的 cellForRowAtIndexPath 中的 NSRangeException

ios - UITableView 显示 'phantom cell'

ios - ONLongPressGesture 作用于移动而不是长按

swift - 准备函数不通过segue调用

ios - 在 SpriteBuilder COCOS2d 3.4 中添加 UIViewController

ios - UITableViewController 的 didSelectRowAtIndexPath 不起作用

ios - Swift 中没有 dequeueReusableCellWithIdentifier 的自定义表格单元格

ios - 防止在表格 View Swift 中重用自定义单元格

IOS/objective-C : Detect Button Press in Custom Tableview Cell?

ios - 如何使用自定义标题将单元格排列成部分?