swift - How to connect UITableViewCell button to perform UITableView functions? -
i have app utilizes tableview contains prototype cell. prototype cell contains few labels , button (connected cell's uitableviewcell file). if that's not clear, mean:
class contactscell: uitableviewcell { //this custom cell used prototype cell tableview @iboutlet weak var firstnamelabel: uilabel! @iboutlet weak var lastnamelabel: uilabel! @iboutlet weak var numberlabel: uilabel! @iboutlet weak var indexpathrowlabel: uilabel! @iboutlet weak var replacecontactbuttonui: uibutton! @ibaction func replacecontactbuttontapped(_ sender: any) { //this button in question }
the reason because i'd each cell in tableview have of labels (and button), each row having different values in labels. these values handled cellforrowat
tableview function, via system follows:
pressing button (not button in cells talking about... different button @ top of vc, outside of tableview) brings contact picker:
func selectcontact(){//allows user bring contactpicker let contactpicker = cncontactpickerviewcontroller() contactpicker.displayedpropertykeys = [cncontactgivennamekey, cncontactfamilynamekey, cncontactphonenumberskey, cncontactimagedatakey] contactpicker.delegate = self contactpicker.predicateforenablingcontact = nspredicate(format: "phonenumbers.@count >= 0") contactpicker.predicateforselectionofcontact = nspredicate(format: "phonenumbers.@count >= 1") self.present(contactpicker, animated: true, completion: nil) } @ibaction func selectcontactsbutton(_ sender: any) { selectcontact() //button brings picker allow selection of contact }
selecting 1 of contacts updates array used tableview update value of labels:
func contactpicker(_ picker: cncontactpickerviewcontroller, didselect contactproperty: cncontactproperty) { contactarray.append(contactproperty.contact) print(contactarray)// contactarray = [cncontact]() contactstableview.reloaddata()// reloads value of labels dismiss(animated: true) }
these work intended. however, run bit of issue button in each cell. button intended allow replacement of cncontact
in tableview @ row of button pressed... (ie if there 4 rows, there 4 "replace" buttons. clicking "replace" button of third row replaces cncontact
@ 2nd index of contactarray
). issue is, though. since button, , action, lives in uitableviewcell's swift file, , not in tableviewcontrollers, doesn't have access contactarray
remove , append, nor picker functions allow selection of new contact, nor tableview run .reloaddata()
. how 'give access' uitableviewcell's swift file utilize things tableview controller part of/connected to?
note possible entire premise of question wrong , should've placed button's action elsewhere (not in uitableviewcell's swift file). if case, should put it?
since have information in viewcontroller
, suggest moving @ibaction
viewcontroller
well. if worried referencing correct cell, can set tag
value of uibutton
in cellforrow
method. in @ibaction
, can use value access correct index in array cell itself
Comments
Post a Comment