我试图让我的应用程序有一个向上投票/向下投票系统类似reddit,这意味着我的表中的每个单元格在单击按钮时只需改变自己的数据就会有不同的反应。我创建了一个自定义单元格类并相应地设置了所有内容,但由于某些原因,当我运行我的应用程序时,它会在您按下单元格中的按钮时崩溃。
这是我的代码(我试图编辑掉任何不必要的部分,但如果你想看到整件事情,请告诉我):
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
}
}
(UPDATE) 我的错误信息:
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)
答案 0 :(得分:1)
Selector("upVoteAction")
和
func upVoteAction(sender: UIButton)
不相同,所以它根本找不到那个方法。您需要将前者的更改添加到
Selector("upVoteAction:")
因为您的实际方法有一个参数。
编辑根据@ rmaddy的评论,swift 3方式会是这样的:
#selector(upVoteAction(sender:))
答案 1 :(得分:0)
答案 2 :(得分:0)
您的单元格已经有自定义单元格类,因此我建议为该类中的按钮创建IBActions。在Interface Builder中,按住Ctrl键从按钮拖动到类文件,然后在那里定义操作。
这样您就不必将代码基于按钮的不同标记。