Reputation: 2249
I have a button on my table view cell. I am returning numberOfRowsInSection
as return arrayname.count
. therefore i get the same number of buttons as the array count.
I trigger a notification using that button, when the user presses that button again the notification gets canceled. It is a location based notification.
It works fine if if there is only one button in the table view. But when I return 2 or more cells, I can't seem to figure out how to distinguish between the button events without one affecting the other. Example - there are two cells in the table and two buttons. One for location 'a' and one for location 'b'. I want a notification when i enter region 'a' so i press the button corresponding to location 'a'. but because button 'b' is not pressed the notification doesn't get scheduled. it works fine only if all the buttons are in the same state. but,how can i solve this problem ?
`button.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
button.setImage(UIImage(named: "a.png")!, forState: .Normal)
button.tag = 999
func pressed(sender: UIButton!) {
if sender.tag == 999
{ sender.setImage(UIImage(named: "b.png")!, forState: .Normal)
sender.tag = 0 } else
{ sender.setImage(UIImage(named: "a.png")!, forState: .Normal) sender.tag = 999 }
}`
I am already using this code to switch images on button press.
Upvotes: 0
Views: 233
Reputation: 254
/*Buy settings the tag to your button you can identify which button is selected */
//create a button globally in your view controller
uibutton*menu
// add the below code in your cellForRowAtIndexPath method
[menus addTarget:self action:@selector(menuclick:) forControlEvents:UIControlEventTouchUpInside];
menus.tag=indexPath.row+1;
//identify button using button sender
(void)menuclick:(id)sender
{
NSInteger tagid=[sender tag];
if(checkbutton.isSelected)
{
//you can identify which button get selected
switch (tagid)
{
case 1:
//button 1 is selected
break;
case2:
//button 2 is selected
break;
default:
break;
}
[menu setSelected:NO];
}
else
{
//you can identify which button get unselected
switch (tagid)
{
case 1:
//button 1 is selected
break;
case2:
//button 2 is selected
break;
default:
break;
}
[menu setSelected:YES];
}
}
Upvotes: 0
Reputation: 6282
I see no connection between the problem that you have and cell reusing.
The easiest way to detect which button is pressed is to add line:
button.tag = indexPath.row
to your cellForRowAtIndexPath
function. Then in the function which is triggered by your buttons add:
func buttonPressed(sender:UIButton) {
let selectedItem = arrayname[sender.tag] // this gives you selected item from array
// or you can do
if sender.tag == 0 {
// button in first cell pressed
}
}
Upvotes: 1