2014-04-10 2 views
0

Tableview의 모든 셀에 버튼을 표시하는 데 문제가 있습니다.TableViewCell에서 버튼이 올바르게 표시되지 않습니다.

여기 내 코드입니다 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"NotificationCell"; 
    NotificationCell *cell = (NotificationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) { 
     cell = [[NotificationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NotificationObject *notification = nil; 
    notification = [_notificationArray objectAtIndex:indexPath.row]; 



    cell.profileImage.image = notification.profileImage; 
    cell.profileImage.layer.cornerRadius = cell.profileImage.frame.size.height /2; 
    cell.profileImage.layer.masksToBounds = YES; 
    cell.profileImage.layer.borderWidth = 0; 
    cell.detailTextView.text = notification.action; 

    UIButton *denyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    //set the position of the button 
    denyButton.frame = CGRectMake(cell.frame.origin.x + 285, cell.frame.origin.y + 20, 23, 23); 
    [denyButton setBackgroundImage:[UIImage imageNamed:@"DenyRequest.png"] forState:UIControlStateNormal]; 
    [denyButton addTarget:self action:@selector(denyButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    denyButton.backgroundColor= [UIColor clearColor]; 
    denyButton.tag = 1000 + indexPath.row; 
    [cell.contentView addSubview:denyButton]; 

    acceptButton.frame = CGRectMake(cell.frame.origin.x + 240, cell.frame.origin.y + 20, 23, 23); 
    [acceptButton setBackgroundImage:[UIImage imageNamed:@"AcceptRequest.png"] forState:UIControlStateNormal]; 
    [acceptButton addTarget:self action:@selector(AcceptButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    acceptButton.backgroundColor= [UIColor clearColor]; 
    acceptButton.tag = 1000 + indexPath.row; 
    [cell.contentView addSubview:acceptButton]; 


    return cell; 
} 

난 데 문제는 버튼은 상단 셀에 표시되는 것입니다.

죄송 합니다만 이것은 간단한 실수 일 뿐이며 객관적으로 새로운 것입니다. c!

답변

1

버튼의 프레임이 셀의 프레임을 고려하지 않도록 단추를 셀의 내용보기에 추가합니다 (전달하는 값은 절대 값이 아닌 셀의 프레임을 기준으로합니다). 따라서 프레임 설정은 다음과 같아야합니다.

denyButton.frame = CGRectMake(285, 20, 23, 23); 
acceptButton.frame = CGRectMake(240, 20, 23, 23); 

다른 문제가 있습니다. 스크롤하여 셀을 재사용하면 단추가 이미있는 셀에 단추가 추가됩니다. 그래서 if-else 절에 새로운 셀을 추가하기 전에 셀에 이미 버튼이 있는지 확인하거나 버튼을 제거하는지 확인해야합니다 (개인적으로는 스토리 보드에 셀을 만들뿐 아니라 처음부터이 모든 코드를 사용해야합니다.)

관련 문제