2012-07-23 124 views
0

我很难找出这一个。我已经设置了持有一个UIImage和的UILabel,和我的UITableViewCell类的定制UIControl类拥有两个这些UIControls的(leftProduct,& rightProduct)期间自定义UIControls与故事板

@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 

图像和标签填充使用init方法cellForRowAtIndexPath,他们通过很好。我有一个与Storyboard中的UIControls相关的目标操作,但是productClicked:方法似乎没有被调用。我试图改变它以编程方式添加目标行动,没有运气。

但是,如果我将一个alloc/init添加到代码中,那么productClicked:方法会正确触发,但不幸的是UILabel和UIPhoto现在在屏幕上显示为空。由于UIControls是在Storyboard中设计的,我认为我不应该自己执行alloc调用,但TableViewController似乎并不喜欢它没有被调用。我试过在[[FeaturedTableCell alloc] init]中调用alloc,但它没有效果。

cellForRowAtIndexPath的内容: cellIdentifier = @“Featured Row”;

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

想通了。在我initWithProduct方法,我包括

self = [super init]; 

我评论了这条线和Touch的内心活动开始正常射击。