【发布时间】:2012-04-01 06:35:42
【问题描述】:
我有一个UITableViewCell,我将添加五个UIButtons。我以编程方式创建按钮、添加动作和图像。按钮放置在正确的位置,操作有效,并且设置了标签,但它们很清晰。图像不显示。
这是我用来创建按钮之一并将它们添加到单元格中的,此代码位于 tableView:cellForRowAtIndexPath: 中:
if ([[self.compCompletion objectForKey:@"Key" isEqualToString:@"YES"]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"Comp-Completed.png"] forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
}
else {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 6, 21, 21);
button.tag = 1;
[button addTarget:self action:@selector(changeCompStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"Comp-Uncompleted.png"] forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:button];
}
我尝试在进程中添加:[button setEnabled:YES];、[button setHidden:NO]; 和 [button setNeedsDislpay];,但没有成功。如何获取显示图像的按钮?
编辑:
我的图像存在,UIImage 已创建,我可以将UIImage 从UIButton 中提取出来并将其保存到文件中。我已经检查了控制外观的UIButton 的每个属性并更改了它们。我还尝试了UIButtonTypeRoundedRect 和UIButtonTypeAddContact 类型的空UIButton。我现在相信这与UITableViewCell 不喜欢UIButtons 或我将UIButton 添加到UITableViewCell 的方式有关。
【问题讨论】:
-
您确定图像名称正确(包括大小写)并且图像文件包含在您的项目中吗?
-
是的,图片名称是正确的,我在不同的 UITableView 中使用相同的图片作为附件视图
-
您确定该单元格显示按钮吗?尝试将其设置为 bacgroundColor 以确保...
标签: ios uitableview cocoa-touch uibutton uikit