[英]How to add image to delete button when we swipe on tableview cell
當我們在tableview單元格上滑動時,我已經實現了代碼來獲取刪除按鈕(請參閱第一張圖片)。 我想自定義它意味着我想添加圖像來代替刪除按鈕(見第二張圖片)。 我用谷歌搜索它,但我沒有得到任何方法或代碼。 每個顯示正常刪除按鈕的地方。 如何添加圖像以刪除按鈕。 任何想法!請幫助我。
將該按鈕更改為其他內容可能不是一個好主意,用戶希望行為保持一致。但是,您可以在自定義單元格中實現此方法。 用戶執行滑動操作時將調用此方法:
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
for (UIView *subview in self.subviews)
{
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[deleteBtn setImage:[UIImage imageNamed:@"delete.png"]];
[[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
[deleteBtn release];
}
}
}
}
注意:在實現自定義之前,我更願意瀏覽Apple的HIG 。
您可以在自定義單元格上添加自定義按鈕和自定義圖像。啟動隱藏此按鈕,然后在單元格(右側或左側)上添加滑動手勢。 (這里是自定義單元類)
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeLeft];
現在處理手勢只是隱藏unhidde自定義刪除按鈕
-(void) handleGesture:(UIGestureRecognizer*)gestureRecognizer{
if([self.deleteBtn isHidden])
{
[self.deleteBtn setHidden:NO];
}else{
[self.deleteBtn setHidden:YES];
}
}
你也可以把兩個滑動手勢一個用於顯示按鈕,另一個用於隱藏按鈕。 我希望這可以幫助你。
我用下面的代碼解決了我的問題,但我不知道蘋果是否會接受。 我在自定義單元格中使用了小UIView(100x40)並在主UIViewController中滑動單元格時跟蹤它。 我在“CellForRowAtIndexPath”方法中編寫了下面的代碼,並且還實現了方法。
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:swipeGestureLeft];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGestureRight:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[cell addGestureRecognizer:swipeGesture];
-(void)handleSwipeGestureLeft:(UIGestureRecognizer *)gestureRecognizer{
NSLog(@"swipe left");
self.cell.UIViewObj.hidden= Yes;
}
-(void)handleSwipeGestureRight:(UIGestureRecognizer *)gestureRecognizer{
NSLog(@"swipe reght");
CGPoint swipeLocation = [gestureRecognizer locationInView:tblView];
NSIndexPath *swipedIndexPath = [tblView indexPathForRowAtPoint:swipeLocation];
NSLog(@"swipedIndexPath %d",[swipedIndexPath row]);
self.cell = (CustomCell*)[tblView cellForRowAtIndexPath:swipedIndexPath];
self.cell.UIViewObj.hidden= No;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.