2

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 ");
 }
4

3 回答 3

1

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

Good Luck

T

于 2012-06-12T15:51:56.380 回答
0

Do this as forgot to specify Control Event:

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

Hope helpful

于 2012-06-12T06:56:26.683 回答
0

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.

于 2012-06-12T07:00:33.300 回答