2010-12-31 8 views
0

したがって、ユーザーに文字列を表示する単一のUITableViewを持つビューコントローラがあります。しかし、通常のUIButtonを使用してこのオブジェクトをIDで選択させる方法が必要でしたので、サブビューとして追加して、clickイベントは期待通りに機能します。UIButtonのint値を渡す方法UITableViewの内部をクリック

問題はこれです - このクリックイベントにint値を渡すにはどうすればよいですか?私はbutton.tagやbutton.accessibilityhintのようなボタンの属性をあまり使わずに使ってみました。どのように専門家はこのようなint値を渡すのですか?

また、プロセスの後半でintから実際の[x intValue]を取得することも重要です。しばらく前に私はこれを単純に行うことができると思った。

NSLog(@ "実際に選択されたハットは%d"、[obj.idValue intValue]);

しかし、これは動作していません(どのように実際のint値を変数から直接引き出すことができますか?)。

私が持っている作業コードは、私は全く異なる方法でこれに行くか

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if ([self.hats count] > 0) { 
     Hat* obj = [self.hats objectAtIndex: [indexPath row]]; 

    NSMutableString* fullName = [[NSMutableString alloc] init]; 
    [fullName appendFormat:@"%@", obj.name]; 
    [fullName appendFormat:@" %@", obj.type]; 

    cell.textLabel.text = fullName; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

    //add the button to subview hack 
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(50, 5, 50, 25); 
    [button setTitle:@"Select" forState:UIControlStateNormal]; 
    button.backgroundColor = [UIColor clearColor]; 
    button.adjustsImageWhenHighlighted = YES; 

    button.tag = obj.idValue; //this is my current hack to add the id but no dice :(

    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 

    [cell.contentView addSubview:button]; 
    //hack 
    } 

    return cell; 
} 

- (void)action:(id)sender { 
    int* hatId = ((UIButton *)sender).tag; //try to pull the id value I added above ... 

    //do something w/ the hatId 
} 
+0

NSLog(@ "実際に選択されたハットは%dでした"、[obj.idValue intValue]);うまくいかないので、決して設定しないでください。私はタグを設定し、送信者からアクションメソッドでそれを読むことで同じことをするので、私のためにそれは動作します。 –

+0

アクセサリボタンと「選択」ボタンの両方が必要ですか?アクセサリボタンをタップすると、accessoryButtonTappedForRowWithIndexPathが呼び出され、タグ付けは不要です。 – Anna

+0

私は帽子の詳細を見るためにアクセサリボタンを使用しています。チェックアウトのために特定の帽子をカートに追加するだけです。 –

答えて

0

最終的な解決策は、いくつかの変更のとおりです

1)の代わりにint型を使用しての私が作ったコメントの数に(感謝)のNSNumberに切り替え も私は問題の多くをするint型を発見しました私が新しいのでメモリ管理については、(

2.)タグにIDを設定するのではなく、コメントに記載されているいくつかの概念を使用し、オブジェクト全体をプッシュしてプルダウンしたときに詳細を取得しましたそれは後の行動から。ここで

はその後、私はオブジェクトに

if ([self.hats count] > 0) { 
     Hat* obj = [self.hats objectAtIndex: [indexPath row]]; 

    NSMutableString* fullName = [[NSMutableString alloc] init]; 
    [fullName appendFormat:@"%@", obj.name]; 
    [fullName appendFormat:@" %@", obj.type]; 

    cell.textLabel.text = fullName; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

    //add the button to subview hack 
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(50, 5, 50, 25); 
    [button setTitle:@"Select" forState:UIControlStateNormal]; 
    button.backgroundColor = [UIColor clearColor]; 
    button.adjustsImageWhenHighlighted = YES; 

    button.tag = obj; 

    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 

    [cell.contentView addSubview:button]; 
    //hack 
    } 

    return cell; 
} 

を設定し、ここで私は、オブジェクトを引き出す改訂アクションで改訂された方法で、私はおよそ

- (void)action:(id)sender { 
    Hat* obj = ((UIButton *)sender).tag; 
    NSNumber* hatId = obj.idValue; 
    //then if I wanted the actual value of hatId I could do 
    NSLog(@"print the hatId out as is %d", [hatId intValue]); 
} 

一つの最後のコメントをしたい任意のプロパティですボタンの再利用は言及されています - 私はまだこの問題を見つけようとしていませんが、私はまだ各テーブルセルにカスタムボタンを持つことができる別のソリューションに興味があります。

2
NSInteger hatId = ((UIButton *)sender).tag; 
+0

私はあなたが必要と思う* intを使用する場合 –

1

を下回っています。私はキーとしてタグを持つボタンオブジェクトを含むNSMutableDictionaryを持っています。おそらくテーブルのデータソースにあります。そのボタンやタグで何かが起こると、あなたはどちらか一方に簡単に乗ることができます。次に、指定されたボタンのタグが必要なときは、辞書のallKeysForObjectメソッドを使用できます。これはオブジェクトごとに1つしかない可能性があります。タグのボタンが必要な場合は、ボタンのポインタを取得するためにキー値としてタグを使用できます。これは、タグ値が再利用されず、一意であることを前提としています(ボタンがなくなったときに辞書からクリアする必要がある場合)。

また、UIButtonをサブクラス化し、タグ値をより適切に保存することもできます。私は理想主義者が、これがあなたのハックでどのように動作するようにしようとしているのか、これが答えであるとあなたに言うでしょう。 =)

いくつか役に立っています。

関連する問題