【问题标题】:UITableView can't smootly scrolling with IOS8 in iphone sdk?UITableView 无法在 iphone sdk 中使用 IOS 8 平滑滚动?
【发布时间】:2014-11-18 06:29:14
【问题描述】:

我可以使用 tableview 在每一行中显示多个图像并且可以滚动。这次我在 tableview 中使用 cutomcell。所以当上下滚动有点粘的时候,它需要流动而不粘不切。 IOS7 完美运行,但 IOS8 出现问题,所以任何人都建议我以更好的方式进行此滚动。

我的 cellForRowAtIndexPath 代码如下:

CellHomeFeedViewController *cell = (CellHomeFeedViewController*)[tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    if (cell==nil) {
        cell = [[[CellHomeFeedViewController alloc] init] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];

        cell.lblUsername.textColor=[UIColor colorWithRed:0.0/255.0 green:176.0/255.0 blue:240.0/255.0 alpha:1.0];
        cell.lblTime.textColor=[UIColor colorWithRed:71.0/255.0 green:72.0/255.0 blue:72.0/255.0 alpha:1.0];
        cell.lblDetails.textColor=[UIColor colorWithRed:71.0/255.0 green:72.0/255.0 blue:72.0/255.0 alpha:1.0];

        // Tap GestureRecognizer for User Image
        UITapGestureRecognizer *UserImg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(UserNameORimageGesture:)];
        UserImg.delegate = self;
        UserImg.numberOfTapsRequired = 1;
        cell.imgUserPic.userInteractionEnabled = YES;
        [cell.imgUserPic addGestureRecognizer:UserImg];

        // Tap GestureRecognizer for Name Label
        UITapGestureRecognizer *NameLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(UserNameORimageGesture:)];
        NameLabel.delegate = self;
        NameLabel.numberOfTapsRequired = 1;
        cell.lblUsername.userInteractionEnabled = YES;
        [cell.lblUsername addGestureRecognizer:NameLabel];

        // Tap GestureRecognizer for Time Label
        UITapGestureRecognizer *TimeLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(UserNameORimageGesture:)];
        TimeLabel.delegate = self;
        TimeLabel.numberOfTapsRequired = 1;
        cell.lblTime.userInteractionEnabled = YES;
        [cell.lblTime addGestureRecognizer:TimeLabel];

        UITapGestureRecognizer *lbltotalCmt = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lbltotalCmtPress:)];
        lbltotalCmt.delegate = self;
        lbltotalCmt.numberOfTapsRequired = 1;
        cell.lblTotalComment.userInteractionEnabled = YES;
        [cell.lblTotalComment addGestureRecognizer:lbltotalCmt];
        cell.lblTotalComment.tag=indexPath.row;

        UITapGestureRecognizer *lbltotalFav = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lbltotalFavPress:)];
        lbltotalFav.delegate = self;
        lbltotalFav.numberOfTapsRequired = 1;
        cell.lblTotalFavourite.userInteractionEnabled = YES;
        [cell.lblTotalFavourite addGestureRecognizer:lbltotalFav];
        cell.lblTotalFavourite.tag=indexPath.row;

        cell.imgUserPic.tag=indexPath.row;
        cell.lblUsername.tag=indexPath.row;
        cell.lblTime.tag=indexPath.row;
        cell.BtnCmnt.tag=indexPath.row;
        cell.BtnGraph.tag=indexPath.row;
        cell.BtnStar.tag=indexPath.row;
        cell.btnMore.tag=indexPath.row;
        cell.BtnFollow.tag = indexPath.row;

        [cell.BtnFollow addTarget:self action:@selector(btnFollowListPress:) forControlEvents:UIControlEventTouchUpInside];
        [cell.BtnCmnt addTarget:self action:@selector(BtnCmnt:) forControlEvents:UIControlEventTouchUpInside];
        [cell.BtnGraph addTarget:self action:@selector(BtnGraph:) forControlEvents:UIControlEventTouchUpInside];
        [cell.BtnStar addTarget:self action:@selector(BtnStar:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnMore addTarget:self action:@selector(BtnMore:) forControlEvents:UIControlEventTouchUpInside];
    }

    NSData *newdata = [[[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"album_title"] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSString *mystring=[[NSString alloc] initWithData:newdata encoding:NSNonLossyASCIIStringEncoding];
    if (mystring.length > 200) {
        NSRange range = NSMakeRange(0, 200);
        mystring = [mystring substringWithRange:range];
    }

    if (mystring.length > 0) {
        cell.lblDetails.text = mystring;
    }
    cell.lblDetails.delegate = self;

    cell.lblTotalFavourite.text = [NSString stringWithFormat:@"(%@)",[[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"favourite_count"]];
    cell.lblTotalComment.text = [NSString stringWithFormat:@"(%@)",[[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"comments_count"]];
    cell.lblTime.text = [[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"album_created"];
    cell.lblUsername.text = [[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"user_name"];
    [cell.imgUserPic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",mainImageURL,[[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"thum_image"]]] placeholderImage:[UIImage imageNamed:@"400_loading_img.png"]];

    if ([[[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"favourite_status"] isEqualToString:@"0"]) {
        [cell.BtnStar setImage:[UIImage imageNamed:@"home_fvrte_icon.png"] forState:UIControlStateNormal];
    } else {
        [cell.BtnStar setImage:[UIImage imageNamed:@"home_fvrte_icon_on.png"] forState:UIControlStateNormal];
    }

    if ([[[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"contact_allowed"] isEqualToString:@"1"]) {
        if ([[[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"is_following"] isEqualToString:@"1"]) {
            cell.boolCheck = NO;
            cell.BtnFollow.hidden = YES;
            [cell.BtnFollow setImage:[UIImage imageNamed:@"following_icon.png"] forState:UIControlStateNormal];
        } else {
            cell.boolCheck = YES;
            cell.BtnFollow.hidden = NO;
            [cell.BtnFollow setImage:[UIImage imageNamed:@"hp_add_frnd_icon.png"] forState:UIControlStateNormal];
        }
    } else {
        cell.boolCheck = NO;
        cell.BtnFollow.hidden = YES;
    }

    if ([[[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"has_commented"] isEqualToString:@"1"]) {
        [cell.BtnCmnt setImage:[UIImage imageNamed:@"home_comment_on.png"] forState:UIControlStateNormal];
    } else {
        [cell.BtnCmnt setImage:[UIImage imageNamed:@"home_comment.png"] forState:UIControlStateNormal];
    }

    if ([[[arrAllPost objectAtIndex:indexPath.row] objectForKey:@"album_photo"] count]>0) {
        cell.boolPostType = YES;
        cell.tblSlider.hidden = NO;
        cell.tblSlider.tag = indexPath.row;
        cell.tblSlider.delegate = self;
        cell.tblSlider.dataSource = self;
        [cell.tblSlider reloadData];
    } else {
        cell.boolPostType = NO;
        cell.tblSlider.hidden = YES;
    }

    return cell;

下面的cutomcell代码:

> @implementation CellHomeFeedViewController @synthesize
> imgUserPic,tblSlider; @synthesize
> lblUsername,lblTime,lblDetails,lblTotalFavourite; @synthesize
> BtnCmnt,BtnGraph,BtnStar,btnMore,imgClock,ViewBottom,BtnFollow,btnReadMore;
> @synthesize lblTotalComment,boolCheck,boolPostType;
> 
> - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
>     self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
>     if (self)
>     {
>         appdelegate = (UBUBBLAppDelegate*)[UIApplication sharedApplication].delegate;
>         
>         imgUserPic = [[UIImageView alloc] init];
>         [self.contentView addSubview:imgUserPic];
>         
>         lblUsername=[[UILabel alloc]init];
>         lblUsername.textColor=[UIColor colorWithRed:0.0/255.0 green:176.0/255.0 blue:240.0/255.0 alpha:1.0];
>         lblUsername.font= [UIFont fontWithName:@"calibri" size:14.0f];
>         [lblUsername setBackgroundColor:[UIColor clearColor]];
>         lblUsername.textAlignment = NSTextAlignmentLeft;
>         [self.contentView addSubview:lblUsername];
>         
>         lblTime=[[UILabel alloc]init];
>         [lblTime setBackgroundColor:[UIColor clearColor]];
>         lblTime.font= [UIFont fontWithName:@"calibri" size:10.0f];
>         lblTime.textAlignment = NSTextAlignmentLeft;
>         [self.contentView addSubview:lblTime];
>         
>         appdelegate.lblLinkColor = [UIColor colorWithRed:0.0/255.0 green:176.0/255.0 blue:240.0/255.0 alpha:1.0];
>         appdelegate.lblTxtColor = [UIColor darkGrayColor];
>         
>         lblDetails=[[KILabel alloc]init];
>         lblDetails.font= [UIFont fontWithName:@"calibri" size:14.0f];
>         [lblDetails setBackgroundColor:[UIColor clearColor]];
>         lblDetails.textAlignment = NSTextAlignmentLeft;
>         lblDetails.numberOfLines = 50;
>         lblDetails.textColor = [UIColor blackColor];
>         lblDetails.userInteractionEnabled = YES;
>         [self.contentView addSubview:lblDetails];
>         
>         tblSlider=[[UITableView alloc] init];
>         tblSlider.showsHorizontalScrollIndicator = NO;
>         tblSlider.showsVerticalScrollIndicator = NO;
>         tblSlider.separatorStyle = UITableViewCellSeparatorStyleNone;
>         tblSlider.transform = CGAffineTransformMakeRotation(-M_PI_2);
>         [self.contentView addSubview:tblSlider];
>         
>         imgClock = [[UIImageView alloc] init];
>         imgClock.image = [UIImage imageNamed:@"home_clock_icon.png"];
>         [self.contentView addSubview:imgClock];
>         
>         ViewBottom = [[UIView alloc] init];
>         ViewBottom.backgroundColor = [UIColor clearColor];
>         [self.contentView addSubview:ViewBottom];
>         
>         btnMore = [UIButton buttonWithType:UIButtonTypeCustom];
>         [btnMore setImage:[UIImage imageNamed:@"home_more_icon.png"] forState:UIControlStateNormal];
>         [ViewBottom addSubview:btnMore];
>         
>         BtnCmnt = [UIButton buttonWithType:UIButtonTypeCustom];
>         [BtnCmnt setImage:[UIImage imageNamed:@"home_comment.png"] forState:UIControlStateNormal];
>         [ViewBottom addSubview:BtnCmnt];
>         
>         BtnStar = [UIButton buttonWithType:UIButtonTypeCustom];
>         [BtnStar setImage:[UIImage imageNamed:@"home_fvrte_icon_on.png"] forState:UIControlStateNormal];
>         [ViewBottom addSubview:BtnStar];
>         
>         BtnFollow = [UIButton buttonWithType:UIButtonTypeCustom];
>         [BtnFollow setImage:[UIImage imageNamed:@"following_icon.png"] forState:UIControlStateNormal];
>         [ViewBottom addSubview:BtnFollow];
>         
>         BtnGraph = [UIButton buttonWithType:UIButtonTypeCustom];
>         [BtnGraph setImage:[UIImage imageNamed:@"home_status_icon.png"] forState:UIControlStateNormal];
>         [ViewBottom addSubview:BtnGraph];
>         
>         lblTotalComment=[[UILabel alloc]init];
>         lblTotalComment.font= [UIFont fontWithName:@"calibri" size:12.0f];
>         [lblTotalComment setBackgroundColor:[UIColor clearColor]];
>         [lblTotalComment setTextColor:[UIColor darkGrayColor]];
>         [ViewBottom addSubview:lblTotalComment];
>         
>         lblTotalFavourite=[[UILabel alloc]init];
>         lblTotalFavourite.font= [UIFont fontWithName:@"calibri" size:12.0f];
>         [lblTotalFavourite setBackgroundColor:[UIColor clearColor]];
>         [lblTotalFavourite setTextColor:[UIColor darkGrayColor]];
>         [ViewBottom addSubview:lblTotalFavourite];
>         
>     }
>     return self; }
> 
> -(void)layoutSubviews {
>     [super layoutSubviews];
>     
>     imgUserPic.frame = CGRectMake(5, 5, 36, 36);
>     imgClock.frame = CGRectMake(47, 27, 11, 12);
>     lblTime.frame = CGRectMake(61, 27, 260, 12);
>     
>     NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
>     paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
>     paragraphStyle.alignment = NSTextAlignmentLeft;
>     
>     CGSize  labelSize;
>     
>     NSDictionary * attributes = @{NSFontAttributeName : self.lblUsername.font,
>                                    NSParagraphStyleAttributeName : paragraphStyle};
>     labelSize = [lblUsername.text boundingRectWithSize:(CGSize){250, 17000}
>                                               options:NSStringDrawingUsesFontLeading |
> NSStringDrawingUsesLineFragmentOrigin
>                                            attributes:attributes context:nil].size;
>     [lblUsername setFrame:CGRectMake(47, 7, 250, 18)];
>     
>     CGFloat yOffset = 45.0;
>     NSDictionary * attributes1 = @{NSFontAttributeName : self.lblDetails.font,
>                                    NSParagraphStyleAttributeName : paragraphStyle};
>     labelSize = [lblDetails.text boundingRectWithSize:(CGSize){305, 17000}
>                                               options:NSStringDrawingUsesFontLeading |
> NSStringDrawingUsesLineFragmentOrigin
>                                            attributes:attributes1 context:nil].size;
>     [lblDetails setFrame:CGRectMake(15, yOffset, 305, labelSize.height+30)];
>     
>     //btnReadMore.frame = CGRectMake(240, yOffset+labelSize.height-5, 80, 20);
>     
>     if (boolPostType) {
>         yOffset+=labelSize.height+20;
>         tblSlider.frame = CGRectMake(0, yOffset+10, 320, 182);
>         yOffset+=185;
>         BtnGraph.hidden = NO;
>         if (boolCheck) {
>             BtnCmnt.frame = CGRectMake(1, 0, 40, 40);
>             BtnGraph.frame = CGRectMake(75, 0, 50, 40);
>             BtnStar.frame = CGRectMake(141, 0, 40, 40);
>             lblTotalComment.frame = CGRectMake(35, 5, 40, 30);
>             lblTotalFavourite.frame = CGRectMake(174, 5, 40, 30);
>             btnMore.frame = CGRectMake(270, 0, 50, 40);
>             BtnFollow.frame = CGRectMake(213, 0, 50, 40);
>         } else {
>             BtnCmnt.frame = CGRectMake(1, 0, 40, 40);
>             BtnGraph.frame = CGRectMake(95, 0, 50, 40);
>             BtnStar.frame = CGRectMake(186, 0, 40, 40);
>             lblTotalComment.frame = CGRectMake(35, 5, 40, 30);
>             lblTotalFavourite.frame = CGRectMake(219, 5, 40, 30);
>             btnMore.frame = CGRectMake(270, 0, 50, 40);
>         }
>     } else {
>         yOffset+=labelSize.height+10;
>         BtnGraph.hidden = YES;
>         if (boolCheck) {
>             BtnCmnt.frame = CGRectMake(1, 0, 40, 40);
>             BtnStar.frame = CGRectMake(97, 0, 40, 40);
>             lblTotalComment.frame = CGRectMake(35, 5, 40, 30);
>             lblTotalFavourite.frame = CGRectMake(130, 5, 40, 30);
>             btnMore.frame = CGRectMake(270, 0, 50, 40);
>             BtnFollow.frame = CGRectMake(195, 0, 50, 40);
>         } else {
>             BtnCmnt.frame = CGRectMake(1, 0, 40, 40);
>             BtnStar.frame = CGRectMake(136, 3, 30, 30);
>             lblTotalComment.frame = CGRectMake(35, 5, 40, 30);
>             lblTotalFavourite.frame = CGRectMake(165, 10, 35, 15);
>             btnMore.frame = CGRectMake(270, 0, 50, 40);
>         }
>     }
> 
>     ViewBottom.frame = CGRectMake(0, yOffset+15, 320, 40); }
> 
> -(CGFloat)requiredHieghtForCellInTableView:(UITableView *)tableView {
>     NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
>     paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
>     paragraphStyle.alignment = NSTextAlignmentJustified;
>     
>     CGSize  labelSize;
>     
>     CGFloat yOffset = 45.0;
>     NSDictionary * attributes = @{NSFontAttributeName : self.lblDetails.font,
>                                   NSParagraphStyleAttributeName :paragraphStyle};
>     labelSize = [lblDetails.text boundingRectWithSize:(CGSize){305, 17000}
>                                               options:NSStringDrawingUsesFontLeading |
> NSStringDrawingUsesLineFragmentOrigin
>                                            attributes:attributes context:nil].size;
>     
>     yOffset+=labelSize.height;
>     
>     if (boolPostType)
>         yOffset+=205;
>     else
>         yOffset+=10;
>     
>     yOffset+=60;
>     
>     return yOffset; }
> 
> @end

【问题讨论】:

  • TL,没读过(很多)。但是,我在单元格中看到了 NSDataNSString 的转换。我不知道这个数据(字符串)到底有多大,但它肯定会影响性能。也许只是你问题的一部分,但它会影响。为什么不将NSData 转换一次,然后将字符串本身存储在您的数组中?

标签: uitableview ios8 smooth-scrolling


【解决方案1】:

if (cell==nil

删除此 if 语句并继续。那条线导致问题

享受编码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 2014-11-25
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多