したがって、ユーザーに文字列を表示する単一の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
}
NSLog(@ "実際に選択されたハットは%dでした"、[obj.idValue intValue]);うまくいかないので、決して設定しないでください。私はタグを設定し、送信者からアクションメソッドでそれを読むことで同じことをするので、私のためにそれは動作します。 –
アクセサリボタンと「選択」ボタンの両方が必要ですか?アクセサリボタンをタップすると、accessoryButtonTappedForRowWithIndexPathが呼び出され、タグ付けは不要です。 – Anna
私は帽子の詳細を見るためにアクセサリボタンを使用しています。チェックアウトのために特定の帽子をカートに追加するだけです。 –