我有一个自定义 UITableViewCell,其中包含一个 UIButton,并且单元本身响应 UIControlEventTouchUpInside。该单元在 iPhone 模拟器和除 iPhone5 之外的所有版本的 iPhone 中都表现良好。由于某种原因,在 iPhone5 上,电池仅响应电池最底部的触摸。事实上,用户要真正让单元格响应触摸是极其困难的,他们必须在单元格底部附近多次点击才能使其响应。此外,UIButton 在 iPhone5 上根本没有响应。我还应该补充一点,这个错误是在应用程序经过优化以适应 iPhone5 更大的视网膜屏幕之后才开始发生的。我倾向于认为这与电池的尺寸有关,但它在 iPhone 5 上的外观看起来完全正确。我还尝试通过创建一个单独的 NIB 来在使用 iPhone5 时进行实例化来解决此问题,该方法利用了“自动布局”Interface Builder 功能,但这没有解决任何问题。
以下是在 ViewController.m 中实例化自定义 UITableView 单元的代码:
// Configure the cell...
switch (indexPath.section) {
case 0: // Full name
{
UINib *nib = [UINib nibWithNibName:@"ProfileNameCell" bundle:nil];
cell = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
[[(ProfileNameCell *)cell nameLabel] setText:[NSString stringWithFormat:@"%@ %@",
[[[Engine sharedInstance] currentUser] firstname],
[[[Engine sharedInstance] currentUser] lastname]]];
[[(ProfileNameCell *)cell imageButton] addTarget:self
action:@selector(selectUserPhoto:)
forControlEvents:UIControlEventTouchUpInside];
if ([[[Engine sharedInstance] currentUser] photoURL] != nil) {
[(ProfileNameCell *)cell setImageForURL:
[NSURL URLWithString:[[[Engine sharedInstance] currentUser] photoURL]]];
}
break;
}...
这是 View.m 的代码:
@implementation ProfileNameCell
@synthesize imageView=imageView_;
@synthesize imageButton;
@synthesize nameLabel;
- (void)awakeFromNib {
[(CALayer *)[self.imageView layer] setCornerRadius:5.0];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
[(CALayer *)[imageView_ layer] setCornerRadius:5.0];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)setImageForURL:(NSURL *)url {
[NSThread performBlockInBackground:^{
//Code that is executed when the UIButton is pressed...
}
- (void)dealloc {
[imageView_ release];
[imageButton release];
[nameLabel release];
[super dealloc];
}
@end
最佳答案
听起来像是包含单元格的 TableView 的 super View 自动调整大小的问题(不是单元格本身,也可能不是 TableView )。检查所有 View 是否自动调整大小或在代码中调整大小以利用完整的可用高度。您还可以在显示单元格时添加断点 (tableView:didEndDisplayingCell:forRowAtIndexPath:
),然后使用 gdb 调查 super View 及其框架 (po [cell superview]
)。
关于ios - 自定义 UITableViewCell 仅在 iPhone5 上限制用户触摸响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573492/