【发布时间】:2016-03-13 04:55:45
【问题描述】:
对于第 100 万次提出这个问题,我深表歉意,我已经阅读了其他相同问题的问题,但我找不到问题的答案!
我制作了一个 UITapGestureRecognizer 无法正常工作,有人可以看看我的代码并告诉我我做错了什么吗?
-(void) formatCellForY: (CGFloat) y{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat cellHeight = 70;
self.cell = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"Cell"]];
[self.cell setFrame:CGRectMake(20, y, screen.size.width - 40, cellHeight)];
self.cell.backgroundColor = [UIColor grayColor];
self.cell.userInteractionEnabled = YES;
self.cell.layer.shadowColor = [UIColor blackColor].CGColor;
self.cell.layer.shadowOffset = CGSizeMake(0, 1);
self.cell.layer.shadowOpacity = 1.0;
self.cell.layer.shadowRadius = 1.0;
self.cell.layer.cornerRadius = 1.0;
self.cell.clipsToBounds = NO;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, self.cell.frame.size.width - 40, cellHeight*(.4))];
titleLabel.numberOfLines = 32;
titleLabel.text = self.program.programName;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.userInteractionEnabled = YES;
[self.cell addSubview:titleLabel];
UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height, titleLabel.frame.size.width, cellHeight - (titleLabel.frame.origin.y+ titleLabel.frame.size.height))];
explanationLabel.text = self.program.programDescription;
explanationLabel.numberOfLines = 3;
explanationLabel.textColor = [UIColor whiteColor];
explanationLabel.font = [UIFont systemFontOfSize:10.0];
explanationLabel.userInteractionEnabled = YES;
[self.cell addSubview:explanationLabel];
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToInfoPage:)];
self.tap.numberOfTapsRequired = 1;
self.tap.numberOfTouchesRequired = 1;
self.tap.enabled = YES;
[self.cell addGestureRecognizer:self.tap];
NSLog(@"%@", self.tap);
}
这是我用来将单元格添加到屏幕的代码。
for (CKRecord *record in records) {
SBHProgram *program = [SBHProgram programForRecord:record];
SBHCell *cell = [SBHCell cellForProgram:program andY:90*i];
i++;
[scrollView addSubview:cell.cell];
}
【问题讨论】:
-
您已经创建了“单元格”,但您是否已将“单元格”添加到任何子视图中?我看到你已经在单元格中添加了子视图......
-
是的,单元格被添加到 MainViewController 的主视图中
-
@Nshweky:这适用于我的模拟器!
-
@Nshweky:您是在其父视图区域之外添加单元格吗?
-
我不这么认为,只是一个简单的 [self.view addSubview: obj.cell];
标签: ios objective-c uitapgesturerecognizer