我很难搞清楚这一个。我有一个自定义UIControl类来设置UIImage和UILabel,而我的UITableViewCell类包含两个UIControls(leftProduct,& rightProduct)
@interface FeaturedProductControl : UIControl
@property (strong, nonatomic) IBOutlet UIImageView *featuredProductPhoto;
@property (strong, nonatomic) IBOutlet UILabel *featuredProductDescription;
@property (strong, nonatomic) Product *featuredProduct;
- (id)initWithProduct:(Product *)product;
@end
@interface FeaturedTableCell : UITableViewCell
@property (strong, nonatomic) IBOutlet FeaturedProductControl *leftProduct;
@property (strong, nonatomic) IBOutlet FeaturedProductControl *rightProduct;
@end
在cellForRowAtIndexPath期间使用init方法填充图像和标签,并且它们正好通过。我有一个与故事板中的UIControl相关联的目标操作,但似乎没有调用productClicked:方法。我已经尝试将其更改为以编程方式添加目标操作,但没有运气。
但是,如果我在代码中添加了alloc / init,那么productClicked:方法会正确触发,但不幸的是UILabel和UIPhoto现在在屏幕上显示为空。由于UIControls是在Storyboard中设计的,我想我不应该自己做alloc调用,但TableViewController似乎并不喜欢它没有被调用。我试过在[[FeaturedTableCell alloc] init]中调用alloc,但它没有效果。
cellForRowAtIndexPath的内容: cellIdentifier = @“特色行”;
FeaturedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[FeaturedTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
FeaturedRow *featuredRowData = [self.productIndexModel objectAtIndexPath:indexPath]; // This contains the necessary products to fill the Row
Product *leftProduct = [featuredRowData.skuList objectAtIndex:0];
cell.leftProduct = [[FeaturedProductControl alloc] initWithProduct:leftProduct]; // Actions trigger, but no data
// cell.leftProduct = [cell.leftProduct initWithProduct:leftProduct]; // Data is filled in, but no action
// [cell.leftProduct addTarget:self action:@selector(productClicked:) forControlEvents:UIControlEventTouchUpInside]; // the storyboard mapping works without this line of code
if (featuredRowData.skuList.count > 1)
{
Product *rightProduct = [featuredRowData.skuList objectAtIndex:1];
cell.rightProduct = [cell.rightProduct initWithProduct:rightProduct];
// cell.rightProduct = [[FeaturedProductControl alloc] initWithProduct:rightProduct]; // Yes, these two are reversed from the left side code above for testing
[cell.rightProduct addTarget:self action:@selector(productClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.rightProduct.hidden = NO; // right side column is hidden in case the number of products is odd
}
else
{
cell.rightProduct.hidden = YES;
}
[cell setNeedsLayout];
return cell;
知道我做错了什么吗?我试图在故事板中保留尽可能多的初始化和设置,所以我不想回去以编程方式编写整个UIControl。
谢谢大家!
答案 0 :(得分:0)
想出来。在我的initWithProduct
方法中,我包含了
self = [super init];
我注释掉了这一行,Touch Up Inside事件开始正常启动。