我很難找出這一個。我已經設置了持有一個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。
謝謝大家!