axel
axel

Reputation: 422

UitableView Row selection and UIButton Action

I have an UitableView with five rows and a button. I managed to create the checkmark for one row i mean that the user can check only one row.When checks another row the previus one unchecked. Now can someone help on how to connect my button with the selection of row. I tried something but not working.

Here is my code in cellForRowAtIndexPath :

if([self.checkedIndexPath isEqual:indexPath])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

and my button code :

if (reloadSelectedRow==1) {

   PlayViewController * controller = [[PlayViewController alloc] initWithNibName:@"PlayViewController" bundle:nil];
    //controller.countdownLabel.text= score;
    [self presentViewController:controller animated:YES completion:nil];
}

}

the reloadSelectedRow is an int variable that i create it .h and use it in didSelectRowAtIndexPath as:

reloadSelectedRow = [[NSNumber alloc] initWithInteger:indexPath.row];

The problem is when i press the button nothing happens. Thank you in advance.

Upvotes: 1

Views: 2038

Answers (2)

ThananutK
ThananutK

Reputation: 71

For swift version.I have made multi choice answers for each row.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "PersonalTestTC", for: indexPath) as! PersonalTestTC

    cell.questionTitle.text = "some question here"

    cell.choiceButton1.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)
    cell.choiceButton2.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)
    cell.choiceButton3.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)
    cell.choiceButton4.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)
    cell.choiceButton5.addTarget(self, action: #selector(self.choiceButtonPress), for: .touchUpInside)

    cell.choiceButton1.tag = 1
    cell.choiceButton2.tag = 2
    cell.choiceButton3.tag = 3
    cell.choiceButton4.tag = 4
    cell.choiceButton5.tag = 5

    return cell

}

To get which row and which answer have been selected using tag to identify

func choiceButtonPress(sender: UIButton){

    let tbcell : UITableViewCell = sender.superview?.superview as! UITableViewCell
    let getCell : IndexPath = testListTable.indexPath(for: tbcell)!

    print(getCell.row)
    print(sender.tag)

}

For superview , It s depend on how may UIButton you are on top of it.

Upvotes: 1

Magyar Miklós
Magyar Miklós

Reputation: 4272

[YourButton addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchDown];

You could get the pressed button's superView like this, and the index path:

 -(void)ButtonPressed:(id)sender
{    

    //Get the superview from this button which will be our cell
    UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
    NSIndexPath *pathToCell = [tableView indexPathForCell:owningCell];

}

Upvotes: 3

Related Questions