2012-12-10 4 views
0

내보기에 테이블 뷰가 있습니다. 셀은 사용자 정의 셀을 사용하여 작성됩니다. 테이블 뷰 셀에 큰 문자열을 표시해야하므로 Scrollview에 텍스트 Label을 추가했습니다. 또한 사용자가 테이블 뷰 셀을 탭할 때 일부 코드를 실행해야합니다.탭 인식기를 두 드릴 때 표 셀 식별

사용자가 스크롤 뷰 위에 닿을 때 문제가
[cell.textLabelLine2 setFrame:CGRectMake(cell.textLabelLine2.frame.origin.x, cell.textLabelLine2.frame.origin.y, 500, cell.textLabelLine2.frame.size.height)]; 
    cell.scrollView.contentSize = CGSizeMake(cell.textLabelLine2.text.length*10 , 10); 
    cell.scrollView.pagingEnabled = NO; 

이의있는 tableView가 선택 메서드가 호출되지 않은 : 아래의 코드를 참조하십시오. 이 문제에 대한 해결책은 스크롤 뷰에 제스처 인식기를 추가하는 것입니다. 그러나이 솔루션에서는 어떤 셀 (또는 제스처 인식기)이 선택되었는지 확인할 방법이 없습니다. 누구든지이 문제에 대한 해결책을 찾는데 도움을 줄 수 있습니까?

답변

1

다음 코드

if(gestureRecognizer.state == UIGestureRecognizerStateBegan) { 
    CGPoint p = [gestureRecognizer locationInView:[self tableView]]; 

    NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:p]; 

    if(indexPath != nil) { 

     UITableViewCell *cell = [[self tableView] cellForRowAtIndexPath:indexPath]; 

        ... 
    } 
} 
1

그것은 일반적으로 스크롤 뷰 내에서 스크롤 뷰를 넣어 나쁜 생각하여 셀을 알 수 있습니다. UITableView는 또한 UIScrollView입니다. 다른 축 (즉, 바깥 쪽 스크롤보기가 세로로 스크롤하고 내부 스크롤이 가로 방향)에서 스크롤하는 경우에만 작동합니다.

특정 시나리오의 경우 직접 선택을 트리거해야합니다. 셀에 대한 참조가 있으면 테이블 뷰에 indexPath를 요청할 수 있습니다. 그런 다음 didSelectRow ...에 대한 대리자 메서드를 직접 호출합니다.

1

스크롤 뷰가있는 솔루션에서는 스크롤 뷰에서 스크롤 할 수 없으므로 gestureRecognizer가 터치를 '가져옵니다'. 그래서 저는 scrollview를 전혀 사용하지 않을 것입니다.

레이블이 같은 내용에 크기를 조정합니다 :

CGSize customTextLabelSize = [cell.customTextLabel.text sizeWithFont:cell.customTextLabel.font constrainedToSize:CGSizeMake(cell.customTextLabel.frame.size.width, 999999)]; 
    cell.customTextLabel.frame = CGRectMake(cell.customTextLabel.frame.origin.x, cell.customTextLabel.frame.origin.y, cell.customTextLabel.frame.size.width, customTextLabelSize.height); 

또한 heightForRowAtIndexPath

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    CGSize cellSize = [bigTextString sizeWithFont:customTextLabel.font constrainedToSize:CGSizeMake(generalCellWidth, 999999)]; 
    return cellSize.height; 
} 

에 그냥 didSelectRowAtIndex 방법을 사용할 수있는이 방법이 구현해야합니다. 방법 :


당신이 정말로는있는 ScrollView를 사용 cellForRowAtIndexPath에서 셀에 버튼을 추가하려면


. 셀만큼 큰 버튼을 확인하고이 같은 버튼 태그를 추가

UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    cellButton.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height); 
    cellButton.tag = indexPath.row; 
    [cellButton addTarget:self action:@selector(cellButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:cellButton]; 

그런 다음 추가 :

-(void)cellButtonAction:(UIButton*)sender 
{ 
    //do something with sender.tag 
}