【发布时间】:2012-07-22 05:47:52
【问题描述】:
我很难弄清楚这一点。我有一个自定义 UIControl 类设置为保存 UIImage 和 UILabel,我的 UITableViewCell 类包含其中两个 UIControl(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 方法填充图像和标签,并且它们通过得很好。我有一个与情节提要中的 UIControls 关联的目标操作,但 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。
谢谢大家!
【问题讨论】: