UITableViewCell 内 UIButton 的 IBAction
我在 Interface Builder 中创建了一个 UITableViewCell,并向其中添加了 UIImageView 和 UIButton。我为 UIButton 提供了一个插座,并将其连接到 IBAction,并在事件集中设置了 touch up。我假设它会调用这个方法,因为一切看起来都已连接:
- (IBAction)pressedCheckbox:(id)sender {
[sender setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
NSLog(@"clicked check");
}
检查连接选项卡,我已正确设置所有内容,插座和操作。我没有收到任何错误,但是在预览应用程序时,单击 UITableViewCell 内的该按钮不会执行任何操作,并且我在日志中没有看到任何内容。
为什么它不允许我单击 UITableViewCell 中的按钮?
编辑:
cellForRowAtIndexPath 的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell = topCell;
break;
case 1:
cell = cell1;
break;
}//end switch
}//end if
return cell;
}
I created a UITableViewCell in Interface Builder which I have added a UIImageView and a UIButton to. I have given the UIButton an outlet, and hooked it up to a IBAction with the touch up inside event set. I assumed it would call this method, as everything looked like it is hooked up:
- (IBAction)pressedCheckbox:(id)sender {
[sender setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
NSLog(@"clicked check");
}
Checking the connections tab, I have everything set correctly, the outlet and the action. And I get no errors, but when previewing the app, clicking on that button inside the UITableViewCell does nothing, and I don't see anything in the logs.
Ideas on why it won't let me click my button which is in a UITableViewCell?
EDIT:
Code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell = topCell;
break;
case 1:
cell = cell1;
break;
}//end switch
}//end if
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也明白了问题所在。我需要为单元格设置不同的高度并使用:
以前我只返回单元格 0 的值,因此它导致我的所有其他单元格出现问题
I also figured out what the problem was. I was needing to set different heights for cells and was using:
Previously I was only returning a value for cell 0, so it was causing issues with all my other cells
你在IB做这个吗?可能值得尝试
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
(在所属视图控制器的 loadView 方法中)看看问题是否存在是IB链接。Are you doing this in IB? It may be worth trying
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
(in the loadView method of the owning view controller) to see if the problem is the IB link.你的意思是这是 UITableViewCell 的子类对吗?您确定您使用的是这个而不是普通的 UITableViewCell 吗?
转到
cellForRowAtIndexPath
方法实现并查找实例化单元格的行。确保将其实例化为您所调用的子类,而不是 UITableViewCell。如果您感到困惑,请发布您的codeForRowAtIndexPath
方法的代码,以便我们可以看到它。You mean that this is a subclass of UITableViewCell right? Are you sure that you are using this instead of the normal UITableViewCell?
Go to your
cellForRowAtIndexPath
method implementation and look for the line where you instantiate the cell. Make sure it's instantiating it to whatever your subclass is called, and not UITableViewCell. If you're confused, post the code for yourcodeForRowAtIndexPath
method so we can see it.