다음 코드는 테이블 셀에 '초대'버튼과 일부 사용자 정보를 표시하는 데 사용됩니다. 그러나 나는 어떻게 내가 세포의 정보에 어떻게 접근 할 수 있는지 확신하지 못한다. 버튼 클릭 메서드에 매개 변수를 전달할 수 없기 때문에 사용자를 다시 초대합니다 ('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