0

在我的UITableView我想为custom accessoryView表中的行添加按钮;这是在UITableViewCell cellForRowAtIndexPath方法中添加的代码:

    UIImage *image = [UIImage imageNamed:@"icon.png"];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 28, 28)]; // tested also initWithFrame:CGRectMake(0, 0, image.size.width, image.size.heigth)
    [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];

    cell.accessoryView = button;
    [button addTarget:self action:@selector(accessoryButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];

这在 iOS 模拟器(iOS 5.1 和 iOS 6.1)上完美运行,我可以在表格的每一行的右侧看到带有 icon.png 图像的自定义 UIButton,我还可以在自定义图像上调用accessoryButtonTapped 事件。但是,如果我在真实设备(带有 iOS 5.1 的 iPhone 4 或带有 iOS 6.1 的 iPad 3rd)上测试我的应用程序,我看不到自定义按钮,它是不可见的,实际上在每行右侧点击仍然会调用 accessoryButtonTapped 事件。我也删除并重新安装了应用程序,我该怎么办?

4

1 回答 1

2

如果您的方法被调用,则表示该按钮在那里。问题出在你的形象上。在设备上运行时,检查图像是否为 nil,以及图像是否已添加到构建目标中。

于 2013-02-14T09:41:50.010 回答