objective-c - UITableViewCell 中的 UIButton 在 iOS7 中不起作用

标签 objective-c uitableview uibutton ios7

我在 UITableViewCell 中有一个 UIButton,自 iOS4 以来一直正常工作,现在自 iOS7 更新后它不再工作。它基本上是一个空盒子的图像。当用户单击图像 (UIButton) 时,图像变为选中框。我没有使用 XIB。有人有什么建议吗?提前致谢。

(我已经尝试过 contentView.userInteractionEnabled = NO;[cell bringSubviewToFront:button] 但这没有用)

这里是一些相关的代码:

- (UITableViewCell *)taskCell:(NSIndexPath *)indexPath table:(UITableView *)localTableView managed:(NSManagedObject *)managedTask dateFormat:(NSDateFormatter *)localDateFormatter{
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [localTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        // Checkbox
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(-4.0f, -3.0f, 48.0f, 48.0f)];
        button.tag=kCellButtonViewTag;
        button.adjustsImageWhenHighlighted=NO;
        [button setImage:[UIImage imageNamed:@"uncheckedPriorityNone.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(toggleCheckedMode:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
        [button release];
        }
}


- (IBAction)toggleCheckedMode:(id)sender{

    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:clickedCell];
    Task *localTask = [self.fetchedResultsController objectAtIndexPath:indexPath];

    UIButton *button = sender;
    [button setImage:[UIImage imageNamed:@"checkedGray.png"] forState:UIControlStateNormal];
}

最佳答案

尝试将 View 添加到单元格本身,而不是内容 View 。

所以

[cell addSubview:button];

代替

[cell.contentView addSubview:button];

关于objective-c - UITableViewCell 中的 UIButton 在 iOS7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318349/

相关文章:

ios - 安全销毁和重新创建单例

iPhone 后退按钮文本 iOS5+ 未显示?

ios - UIButton 随动画显示和消失

ios - 如何将参数传递给外部类方法?

objective-c - method swizzling 和 isa swizzling 是一回事吗?

ios - 如何指定行高?

ios - TableView 在 segue 后显示错误数据

objective-c - 带有 UITextAlignmentLeft 的 iOS UIButton 仍然居中文本?

ios - 像素完美 UILabel

ios - TableView : How to proceed after catching 'NSInternalInconsistencyException' ?