2011-10-31 4 views
1

UITableView의 UITableViewCell에 UIButton이 있습니다. UIButton이 숨겨집니다. 사용자가 특정 UITableViewCell에서 손가락으로 왼쪽으로 쓸어 넘기면 버튼이 나타납니다.uibutton uitableview의 uitableviewcell에 표시 설정

이 코드를 구현하기 위해이 코드를 사용했지만 작동하지만 버튼이 사용자가 손가락을 스 와이프 한 것 이외의 다른 uitableviewcells에 나타납니다!

- (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer 
{ 
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) 
    { 
     UIView *tappedview=[gestureRecognizer.view hitTest:[gestureRecognizer locationInView:gestureRecognizer.view] withEvent:nil]; 

     UIView *contentview1=tappedview.superview; 
     UIView *viewwithtag4=[contentview1 viewWithTag:7009]; 
     UIButton *button2=(UIButton *)viewwithtag4; 

     NSLog(@"swipe left detected"); 

     [button2 setHidden:FALSE]; 
    } 
} 

감사합니다. 감사.

답변

0

성능 향상을 위해 tableCells가 tableView에 의해 재사용되고 있기 때문에 스크롤 후 잘못된 셀에 버튼이 표시되는 경우입니다. 즉

특정 셀에 볼 수있는 버튼을 유지하려면 다음을 수행해야합니다 : 버튼의 상태를 저장 gestureRecognizer에 의해 호출되는 방법에있어서

. 스 와이프 한 셀을 확인한 다음 해당 셀을 채우고있는 클래스/모델에 해당 상태를 저장해야합니다. 즉 데이터 소스. 데이터 소스 객체가 배열에있는 경우 예를 들어, 선 당신의 cellForRowAtIndexPath 방법에서 다음

// in your cellSwiped method and assuming you can traverse the view hierarchy to get 
// the tableViewCell. 
NSIndexPath *theCellIndexPath=[self.tableView indexPathForCell: theTableViewCell]; 
MyDataSourceObject *theDataSourceObject=[dataObjectArray objectAtIndex: theCellIndexPath.row]; 
// The buttonIsVisible ivar for your data source could be name that 
// or something else that is meaningful. Not sure what the button i 
// related to in you objects 
theDataSourceObject.buttonIsVisible=YES // or you could put in code to toggle the value 

을 따라 뭔가를 할 수있는, 당신의 상태에 따라 숨기거나되지 않게 버튼을 설정해야합니다 그 특정 indexPath.

MyDataSourceObject *theDataSourceObject=[dataObjectArray objectAtIndex: indexPath.row]; 
cell.button.hidden=theDataSourceObject.buttonIsVisible; 

return cell; 

+0

예이 스크롤 한 후 발생하는 행운을 빌어 요 ... 당신은 어떤 코드를 제공 할 수 있습니까? – stefanosn

+0

@stefanosn, 의사 코드가 제공됩니다. – timthetoolman

+0

dataObjectArray에 셀의 버튼을 저장하고 있는데 dataObjectArray에서이 버튼을 호출하지만 같은 일이 발생합니다! – stefanosn

관련 문제