2012-01-08 8 views
0

テーブルセルに「招待」ボタンだけでなく、いくつかのユーザー情報を表示するために、次のコードを使用しました。私はどのようにセルの情報にどのようにアクセスできるかわかりません。ボタンをクリックするメソッドにパラメータを渡すことができないため、(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 

} 

答えて

2

UIButtonをサブクラス化して、selectedIndexPathという名前のプロパティを追加して、NSIndexPathを保持することができます。代わりにUIButtonのカスタムボタンを必要とせずに、セルの上にあなたのサブクラスを置き、あなたが

-(void)inviteButtonPressed:(id)sender 
{ 
    NSIndexPath *indexPath = [((MyButton *)b) selectedIndexPath]; 
    GenericUser *user = [userSection objectAtIndex:[indexPath row]]; 


} 

または別のアプローチを行うことができますよりも、ラインMyButton *thisInviteButton = (MyButton *)[cell.contentView viewWithTag:1];

後にインデックスパスを設定します。

を私はわからない
-(void)inviteButtonPressed:(id)sender 
{ 
    UIButton *b = (UIButton *)sender; 
    UITableViewCell* cell = (UITableViewCell*)[[b superview] superview]; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    GenericUser *user = [userSection objectAtIndex:[indexPath row]]; 

} 

は、どのようなアプローチ私はあまり醜い見つけます。

+1

私は一般的に最初のものを好んで使用していましたが、ビューの階層を自由に変更できるので少し剛性が低く、それでも機能します。 –

+0

ええ、私はあなたが正しいと思います。上級ユーザは、サブクラス化が望ましくない場合、カテゴリと関連付けられたindexPathオブジェクトを使用してUIButtonを拡張することができます。 – vikingosegundo

関連する問題