2011-10-31 47 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稱爲保存按鈕的狀態的方法。你將不得不確定已經被刷過的單元格,然後將該狀態保存在你正在填充該單元格的類/模型中。即您的數據源。例如,如果您的數據源是一個對象在數組中,你可以做沿線

// 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 

然後在你的cellForRowAtIndexPath方法的東西,你必須設置按鈕取決於爲國家被隱藏或不那個特定的indexPath。

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

return cell; 

好運

+0

是發生這種情況滾動後......你能提供任何代碼? – stefanosn

+0

@stefanosn,提供僞代碼。 – timthetoolman

+0

我正在dataObjectArray中保存單元格的按鈕,然後我從dataObjectArray調用它,但同樣的情況發生! – stefanosn

相關問題