簡體   English   中英

當我在表格視圖單元格中的uiimageview上添加長按手勢時,Uiimageview沒有顯示正確的圖像

[英]when I am adding Long Press Gesture on uiimageview in Table View Cell .Uiimageview Is not showing With correct Image

我在uiimageview上使用長按手勢代碼,問題是配置文件圖片未顯示正確。我在表格視圖中有50個值,並且在5到6張圖像后,其他單元格圖像將變為Nil。並且配置文件圖片來自Web服務。如果我不加長按,則所有50行將顯示正確的圖像。 這是我的代碼::

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrResultData.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"FishCell" forIndexPath:indexPath];
    // getting the imag which is in prototype cell in storyboard
    UIImageView *cellimg=(UIImageView*)[cell viewWithTag:101];
    cellimg.tag=indexPath.row;
    cellimg.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *gestureRecognizer =   [[UILongPressGestureRecognizer alloc] init];
    gestureRecognizer.delegate = self;
    gestureRecognizer.minimumPressDuration = 0.5;
    [cellimg addGestureRecognizer: gestureRecognizer];
    [gestureRecognizer addTarget:self action:@selector(imgLongPressed:)];

    - (void)imgLongPressed:(UILongPressGestureRecognizer*)sender
    {
        //  UIImageView *view_ =(UIImageView*) sender.view;
        NSLog(@"view tag %ld",sender.view.tag);
        //    CGPoint point = [sender locationInView:view_.superview];
        //
        if (sender.state == UIGestureRecognizerStateBegan){
            Profile_PopUP_Vc *Profile_PopUPVc = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile_PopUP_Vc"];
            Profile_PopUPVc.ImageUrl=[[arrResultData valueForKey:@"picture"]objectAtIndex:sender.view.tag];
            Profile_PopUPVc.strUsername=[[arrResultData valueForKey:@"username"]objectAtIndex:sender.view.tag];
            Profile_PopUPVc.delegate = self;
            [self presentPopupViewController:Profile_PopUPVc animationType:MJPopupViewAnimationFade];
        }
        else if (sender.state == UIGestureRecognizerStateEnded){
            [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
        }
    }
}

錯誤在於以下幾行:

UIImageView *cellimg=(UIImageView*)[cell viewWithTag:101];
cellimg.tag=indexPath.row;

您正在更改圖像視圖標簽,然后,當單元被重用時,它沒有帶有標簽101的視圖,因此您的cellimgnil

從indexView的行中的單元格中的imageView中刪除所有手勢

for (UIGestureRecognizer *recognizer in cellimg.gestureRecognizers) {
    [cellimg removeGestureRecognizer:recognizer];
}

然后添加您在提到的代碼中使用的手勢。 之所以這樣使用,是因為當單元格用於行的單元格被重用時,會選擇錯誤的圖像。

您需要使用視圖的層次結構獲取indexpath,並使用此indexpath而不是其tag值。

UIImageView *view_ =(UIImageView*) sender.view
UITableViewCell *cell = (UITableViewCell *)view_.superview.superview;
NSIndexPath *indexPath = [recipeCollectionView indexPathForCell:cell];
Profile_PopUPVc.ImageUrl=[[arrResultData valueForKey:@"picture"]objectAtIndex:indexPath.row];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM