2011-07-12 10 views
0

カスタムセルでテーブルビューを作成しました。セルには、ラベルとボタンが含まれます。質問は、ボタンを押したときにラベルのテキストをどのように検出するのですか?テーブルビューのカスタムセルボタン

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) { 
    // Initialization code 
     primaryLabel = [[UILabel alloc]init]; 
     primaryLabel.textAlignment = UITextAlignmentLeft; 
     primaryLabel.font = [UIFont systemFontOfSize:11]; 
     primaryLabel.backgroundColor = [UIColor clearColor]; 
     secondaryLabel = [[UILabel alloc]init]; 
     secondaryLabel.textAlignment = UITextAlignmentLeft; 
     secondaryLabel.font = [UIFont systemFontOfSize:9]; 
     secondaryLabel.backgroundColor = [UIColor clearColor]; 
     myImageView = [[UIImageView alloc]init]; 

     btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btn.backgroundColor = [UIColor clearColor]; 

     [self.contentView addSubview:btn]; 
     [self.contentView addSubview:primaryLabel]; 
     [self.contentView addSubview:secondaryLabel]; 
     [self.contentView addSubview:myImageView]; 
    } 

    return self; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    return cell; 
} 
+0

これは難しいことではありませんが、セルの設定方法によって異なります。セル、ラベル、ボタンを作成するコードを表示できますか? – sosborn

+0

私はリマインダー機能も持っています。私はセルの内容をキャッチし、カレンダーのeventcontrollerを設定する必要があります。 – Ulvi

答えて

0

あなたが探している方法がある - addTarget:アクション:forControlEvents:

あなたの例を使用するには、あなたがUIButton、BTNを持っている、とあなたはmyActionと呼ばれるカスタムセルでアクションを持っているとしましょう。コードは次のようになります。

[btn addTarget:self action:myAction forControlEvents:UIControlEventTouchUpInside]; 

myActionはセル内のメソッドなので、labelプロパティに簡単にアクセスできます。あなたが別のコントローラにあなたの行動を置く必要がある場合:この場合

[btn addTarget:otherController action:myAction forControlEvents:UIControlEventTouchUpInside]; 

を、あなたは、コントローラにあなたのセルへの参照を保持する必要があります。おそらく複数のセルがあるので、NSMutableArrayをプロパティとして保持し、テーブルを構築するときにセルを追加することができます。

+0

あなたの答えに感謝します。試してみます。 – Ulvi