swift - App crashes when cell button is clicked -
i'm trying make app have vote / down vote system sorta reddit, means each cell in table had react differently when buttons clicked alter own data. create custom cell class , set accordingly, reason when run app crashes anytime hit buttons in cells.
here's code (i tried edit out unnecessary parts, let me know if you'd see whole thing):
import uikit import firebase import firebasedatabase class findpartiesviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { // store parties var parties = [party]() @iboutlet weak var partytable: uitableview! var refreshcontrol = uirefreshcontrol() override func viewdidload() { super.viewdidload() partytable.delegate = self partytable.datasource = self } // creates number of cells in table func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return parties.count // define cells func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { // make table cells show 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) error message:
2016-11-10 13:35:20.371 lit[16625:2721786] -[lit.findpartiesviewcontroller upvoteaction]: unrecognized selector sent instance 0x7fc40b420240 2016-11-10 13:35:20.375 lit[16625:2721786] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[lit.findpartiesviewcontroller upvoteaction]: unrecognized selector sent 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 uncaught exception of type nsexception (lldb)
selector("upvoteaction") and
func upvoteaction(sender: uibutton) are not same, it's not finding method @ all. need add change former
selector("upvoteaction:") because actual method has parameter.
edit: per @rmaddy's comment, swift 3 way :
#selector(upvoteaction(sender:))
Comments
Post a Comment