我创建了一个自定义UITableViewCell子类,其中包含一个名为testbtn
的按钮。然后,我尝试将该按钮的事件分配给另一个类,但这对我不起作用。你能告诉我哪里出错了吗?
这是我的代码:
- (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 ");
}
检查您的按钮是否连接到 NIB 中的 testBtn 实例变量。
祝你好运
T
在忘记指定控制事件时执行此操作:
[cell.testbtn addTarget:self action:@selector(openLiveData:) forControlEvents:UIControlEventTouchUpInside];
希望有帮助
它在iPhone版本中应该可以正常工作,但在iPad版本中应该是一个问题,正如您在forControlEvents:UIControlStateNormal
代替forControlEvents:UIControlEventTouchUpInside
时提到的。