テーブルセルに「招待」ボタンだけでなく、いくつかのユーザー情報を表示するために、次のコードを使用しました。私はどのようにセルの情報にどのようにアクセスできるかわかりません。ボタンをクリックするメソッドにパラメータを渡すことができないため、(inviteButtonPressedメソッド内の)招待ボタンをクリックしたときに、再びユーザーを呼び出します。誰もボタンのクリック方法でセルの情報にアクセスする方法を教えてもらえますか?セルのコンテンツビューのボタンをクリックした後にセル情報にアクセスする方法は?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = @"SectionsTableIdentifier";
GenericUser *user = [userSection objectAtIndex:row];
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
cell.textLabel.textColor = [UIColor darkGrayColor];
UIButton *inviteButton = [self setupButtonWithTitle:@"Invite" andFrame:CGRectMake(224, (44-24)/2, 56, 24)];
[inviteButton addTarget:self action:@selector(inviteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
inviteButton.tag = 1;
[cell.contentView addSubview:inviteButton];
}
UIButton *thisInviteButton = (UIButton*)[cell.contentView viewWithTag:1];
//This is where I will trigger the button press method
[thisInviteButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
UILabel *thisInvitedLabel = (UILabel*)[cell.contentView viewWithTag:2];
cell.textLabel.text = user.name;
cell.detailTextLabel.text = user.email;
if (user.isSelected)
{
thisInviteButton.hidden = YES;
}
else
{
thisInviteButton.hidden = NO;
}
return cell;
}
-(void)inviteButtonPressed:(id)sender
{
//I want to access the cell information here. How can I do it? Basically I want to pass the user information belonging to the cell to this method
}
私は一般的に最初のものを好んで使用していましたが、ビューの階層を自由に変更できるので少し剛性が低く、それでも機能します。 –
ええ、私はあなたが正しいと思います。上級ユーザは、サブクラス化が望ましくない場合、カテゴリと関連付けられたindexPathオブジェクトを使用してUIButtonを拡張することができます。 – vikingosegundo