【发布时间】:2012-06-12 06:51:27
【问题描述】:
我创建了一个自定义 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实例变量。
-
@AdrianAncuta 是的。它没有调用打印到控制台的那个数学模型
-
@timthetoolman 哦!!对不起,我忘记将实例变量链接到我的 XIB
-
你现在可以工作了,谢谢大家
标签: iphone objective-c ios uitableview ios5