0

私はこれを理解するのに苦労しています。UIImageとUILabelを保持するように設定されたカスタムUIControlクラスがあり、UITableViewCellクラスはこれらのUIControlの2つ(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はストーリーボードで設計されているので、自分で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全体を作成することに戻りたくありません。

みんな、ありがとう!

4

1 に答える 1

0

理解した。私のinitWithProduct方法では、

self = [super init];

この行をコメントアウトすると、Touch Up Inside イベントが正常に起動し始めました。

于 2012-07-31T22:21:41.170 に答える