Rahul
Rahul

Reputation: 187

TableCell with buttons and different button action

I am new to iphone and working on a project where a requirement is in a UITableView we have two sections. In first section we have to show address and in second section we have four variable rows which consists of Phone number, Fax, Email, and SMS. If any of the item (phone,fax,email,sms) will not have data then that row will not be visible and each row contains a button. On click on these UIButton should perform the specific function like if I clicked on the button which in phone row then it should connect to phone like wise.

My problem is I am not able to differentiate the UIButton actions as the rows are not fixed. So how to perform the action according to particular row data dynamically. Currently am I able to perform only one action for all rows button.

Upvotes: 0

Views: 113

Answers (3)

himan
himan

Reputation: 459

Give different tag numbers to your UIButtons. You can give tag numbers to your buttons using the IB, then perform the actions accordingly calling the tag values

Upvotes: 0

Bittu
Bittu

Reputation: 676

There are couple of different ways of handling it. I am assuming you have some custom cell that has an UILabel and UIButton on it. First thing is to add tags, like Mundi said.

Second option is you add a method to the custom cell that sets the target and action for the button. When you are setting up your cell based on the data, you can also set the selector for each of those buttons as well.

Upvotes: 0

Mundi
Mundi

Reputation: 80265

Use enums to define your type of cell. Check the enum in your handler.

typedef enum {
   CellTypePhone = 100, 
   CellTypeFax, 
   CellTypeEmail, 
   CellTypeSMS
} CellType;

Use these to tag your cells or buttons, e.g.

cell.tag = CellTypePhone; 
button.tag = CellTypePhone;

Then you know what cell was selected in the button handler.

-(void)buttonPressed:(UIButton*)sender {
    if      (sender.tag == CellTypePhone)     { /* handle phone */ }
    else if (sender.tag == CellTypeFax)       { /* handle fax   */ }
    // etc.
}

Upvotes: 1

Related Questions