单击单元格按钮时应用程序崩溃
App crashes when cell button is clicked
我试图让我的应用程序有一个类似 reddit 的赞成票/反对票系统,这意味着我的 table 中的每个单元格都必须在单击按钮时做出不同的反应以仅改变它们自己的数据。我创建了一个自定义单元格 class 并相应地设置了所有内容,但是由于某些原因,当我 运行 我的应用程序在您点击单元格中的按钮时崩溃。
这是我的代码(我试图删除任何不必要的部分,但如果您想查看整个代码,请告诉我):
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)
首先,尝试说明控制台中打印的错误。
其次,确保您所有的 IBoutlets 都已连接并且没有任何未连接的。如果其中一个插座周围有感叹号,请使用 x 按钮将其删除并重新构建。
您的单元格已经有一个自定义单元格 class,因此我建议为该 class 中的按钮创建 IBAction。在 Interface Builder 中,按住 Ctrl 键从按钮拖动到 class 文件并在那里定义操作。
这样您就不必将代码基于按钮的不同标签。
Selector("upVoteAction")
和
func upVoteAction(sender: UIButton)
不相同,所以它根本找不到那个方法。您需要将前者更改为
Selector("upVoteAction:")
因为你的实际方法有一个参数。
编辑: 根据@rmaddy 的评论,swift 3 方式类似于:
#selector(upVoteAction(sender:))
我试图让我的应用程序有一个类似 reddit 的赞成票/反对票系统,这意味着我的 table 中的每个单元格都必须在单击按钮时做出不同的反应以仅改变它们自己的数据。我创建了一个自定义单元格 class 并相应地设置了所有内容,但是由于某些原因,当我 运行 我的应用程序在您点击单元格中的按钮时崩溃。
这是我的代码(我试图删除任何不必要的部分,但如果您想查看整个代码,请告诉我):
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)
首先,尝试说明控制台中打印的错误。
其次,确保您所有的 IBoutlets 都已连接并且没有任何未连接的。如果其中一个插座周围有感叹号,请使用 x 按钮将其删除并重新构建。
您的单元格已经有一个自定义单元格 class,因此我建议为该 class 中的按钮创建 IBAction。在 Interface Builder 中,按住 Ctrl 键从按钮拖动到 class 文件并在那里定义操作。
这样您就不必将代码基于按钮的不同标签。
Selector("upVoteAction")
和
func upVoteAction(sender: UIButton)
不相同,所以它根本找不到那个方法。您需要将前者更改为
Selector("upVoteAction:")
因为你的实际方法有一个参数。
编辑: 根据@rmaddy 的评论,swift 3 方式类似于:
#selector(upVoteAction(sender:))