2012-01-08 2 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

관련 문제