Bittu
Bittu

Reputation: 371

Assign UIButton event dynamically in UITableViewCell

I created a custom UITableViewCell subclass which contains a button named testbtn. I then tried to assign an event of that button to another class, but that isn't working for me. Please can you tell me where I am going wrong?

Here is my code:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"PatientCell";
    PatientCell *cell = (PatientCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            NSArray *TopLevelOb = [[NSBundle mainBundle] loadNibNamed:@"PatientCell" owner:self options:nil];
            for (id currentlevelob in TopLevelOb) {
                if([currentlevelob isKindOfClass:[PatientCell class]]){
                    cell = (PatientCell *) currentlevelob;
                    cell.test.text = @"some text";
                    cell.HR.text =@"80";
                    cell.ABP.text =@"120/20";
                    cell.SOP2.text =@"95";
                    cell.RR.text=@"98";
                    [cell.testbtn addTarget:self action:@selector(openLiveData:)   
                       forControlEvents:UIControlEventTouchUpInside];// **this event is not fire to openLiveData method**
                    break;
                }
            }
        } else {
            NSArray *TopLevelOb = [[NSBundle mainBundle] loadNibNamed:@"PatientCell_iPad" owner:self options:nil];
            for (id currentlevelob in TopLevelOb) {
                if([currentlevelob isKindOfClass:[PatientCell class]]){
                    cell = (PatientCell *) currentlevelob;
                    cell.test.text = [nameArry objectAtIndex:indexPath.row];
                    cell.HR.text =@"80";
                    cell.ABP.text =@"120/20";
                    cell.SOP2.text =@"95";
                    cell.RR.text=@"98";
                    [cell.testbtn addTarget:self action:@selector(openLiveData:) forControlEvents:UIControlEventTouchUpInside]; 
                    break;
                }
            }     
        }
    }
    return cell;
}

-(IBAction)openLiveData:(id)sender{
     NSLog(@"hello ");
 }

Upvotes: 2

Views: 177

Answers (3)

timthetoolman
timthetoolman

Reputation: 4623

Check to see if your button is wired up to the testBtn instance variable in your NIB.

Good Luck

T

Upvotes: 1

Farrukh Javeid
Farrukh Javeid

Reputation: 634

It should be working fine in the iPhone version but should be a problem in the iPad version as you mentioned forControlEvents:UIControlStateNormal in the place of forControlEvents:UIControlEventTouchUpInside.

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Do this as forgot to specify Control Event:

  [cell.testbtn addTarget:self action:@selector(openLiveData:) forControlEvents:UIControlEventTouchUpInside];

Hope helpful

Upvotes: 0

Related Questions