2012-12-19 7 views
0

UITableView에서만 UIButton을 indexPath.row로 표시하고 싶습니다.UITableView에 상호 UIButton을 표시하십시오.

그러나 UIButton은 모든 셀에 나타납니다.

if (indexPath.row % 2 == 0) { 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(160, 140, 80, 40); 
    [button setTitle:@"button" forState:UIControlStateNormal]; 
    [cell addSubview:button]; 
} 

이 문제를 어떻게 해결할 수 있습니까? 감사합니다. .

+4

'[cell.contentView addSubview : button];' – janusbalatbat

답변

0

먼저 셀의 높이 무엇인지 확인하십시오

당신 때문에 버튼의 높이의 문제에 직면 할 수있다가. Button의 높이보다 크거나 같아야합니다. 또한 코드를이 코드로 바꿉니다.

if (indexPath.row % 2 == 0) { 
     UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(160, 4, 80, 40)]; 
     [button setTitle:@"BtnName" forState:UIControlStateNormal]; 
     [cell.contentView addSubview:button]; 

} 
0

코드는 정확하지만 아주 작은 실수를 한 것입니다. 버튼을 cell에 추가하는 대신 다음과 같이 cellcontentView에 버튼을 추가해보세요.

[cell.contentView addSubview:button]; 
관련 문제