【问题标题】:IOS UITableview Scroll is not smooth in IOS6IOS UITableview Scroll在IOS6中不流畅
【发布时间】:2014-07-12 13:20:30
【问题描述】:

我在 IOS 7 中完成了 IOS 应用程序,它在 IOS 7 中运行良好,然后我开始将我的应用程序转换为 IOS 6,我遇到了很多问题。经过巨大的努力,我纠正了除性能问题之外的几乎所有问题

我正在使用 UITableview 显示所有内容,当我在 Iphone 5c 中测试我的应用程序时,滚动表格视图没有问题。而且我在 ipod 视网膜列表中测试我的应用程序滚动不顺畅。这对我来说是个大问题,也浪费了我的时间

为了说明我的问题,我在下面添加了我的 tableview 单元格代码。请给我一些快速的解决方案

UITableviewCell 代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellidentifier=@"cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];

    if (cell ==nil)
    {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]autorelease];

    }
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    cell.selectionStyle = UITableViewCellEditingStyleNone;

        UIView * contentview = [[[UIView alloc]init]autorelease];
        UIImageView * userimage = [[[UIImageView alloc]init]autorelease];
        UIImageView * itemimageview = [[[UIImageView alloc]init]autorelease];
        UIView * bottomview = [[[UIView alloc]init]autorelease];
        UILabel * imagenameLable = [[[UILabel alloc]init]autorelease];
        UILabel * usernameLable = [[[UILabel alloc]init]autorelease];

        UILabel * itemcostLable = [[[UILabel alloc]init]autorelease];


        UIButton*fancybtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIButton * addrditbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIButton * commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];





        UILabel * noofcommentsLable = [[[UILabel alloc]init]autorelease];
//        [contentBgImageview setImage:[UIImage imageNamed:@"item_bg.png"]];
        [itemimageview setAutoresizesSubviews:YES];
        //    [itemimageview setContentMode:UIViewContentModeScaleAspectFill];

        [itemimageview setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"photos"]objectAtIndex:0]objectForKey:@"item_url_main_350"]]]];
        [itemimageview setContentMode:UIViewContentModeScaleAspectFit];
        [itemimageview setTag:indexPath.row];

        [userimage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"photos"]objectAtIndex:0]objectForKey:@"user_url_main_70"]]]];

        [bottomview setBackgroundColor:[UIColor colorWithRed:200/255 green:54/255 blue:54/255 alpha:0.4]];

        [bottomview setBackgroundColor:[UIColor colorWithRed:255 green:255 blue:255 alpha:1]];

        //    [bottomview setAlpha:0.5];


        [imagenameLable setText:[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"item_title"]];
        [imagenameLable setTextColor:[UIColor blackColor]];
        [imagenameLable setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
        [imagenameLable setBackgroundColor:[UIColor clearColor]];
        [bottomview addSubview:imagenameLable];


        [usernameLable setText:[NSString stringWithFormat:@"@%@",[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"sellername"]]];

        [usernameLable setTextColor:[UIColor grayColor]];
        [usernameLable setFont:[UIFont fontWithName:@"Helvetica" size:10]];
        [usernameLable setBackgroundColor:[UIColor clearColor]];
        [bottomview addSubview:usernameLable];


        [itemcostLable setText:[NSString stringWithFormat:@"%@ %@",delegate.currencyStr,[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"price"]]];
        [itemcostLable setTextColor:[UIColor grayColor]];
        [itemcostLable setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
        [itemcostLable setBackgroundColor:[UIColor clearColor]];
        [bottomview addSubview:itemcostLable];

//        [addrditbtn setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
        if ([[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"liked"]isEqualToString:@"No"])
        {
            [fancybtn setImage:[UIImage imageNamed:@"fantacybtn.png"] forState:UIControlStateNormal];
            [addrditbtn setImage:[UIImage imageNamed:@"addtolist.png"] forState:UIControlStateNormal];
        }
        else
        {
            [fancybtn setImage:[UIImage imageNamed:@"fantacydbtn.png"] forState:UIControlStateNormal];
            [addrditbtn setImage:[UIImage imageNamed:@"addtolist.png"] forState:UIControlStateNormal];
        }


        NSString*itemid=[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"id"];

        if ([delegate.fantacyitemsArray containsObject:itemid]) {
            [fancybtn setImage:[UIImage imageNamed:@"fantacydbtn.png"] forState:UIControlStateNormal];
            [addrditbtn setImage:[UIImage imageNamed:@"addtolist.png"] forState:UIControlStateNormal];
        }

        if ([delegate.unfantacyitemsArray containsObject:itemid]) {
            [fancybtn setImage:[UIImage imageNamed:@"fantacybtn.png"] forState:UIControlStateNormal];
            [addrditbtn setImage:[UIImage imageNamed:@"addtolist.png"] forState:UIControlStateNormal];
        }

        fancybtn.tag=[itemid intValue];
        [fancybtn   addTarget:self action:@selector(fancyBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        addrditbtn.tag=[itemid intValue];

        [addrditbtn   addTarget:self action:@selector(addtolistBtnPressed:) forControlEvents:UIControlEventTouchUpInside];



        [commentBtn setImage:[UIImage imageNamed:@"commentnew.png"] forState:UIControlStateNormal];
        [commentBtn setUserInteractionEnabled:YES];
        commentBtn.tag=indexPath.row;
        [commentBtn   addTarget:self action:@selector(commentBtnPressed:) forControlEvents:UIControlEventTouchUpInside];

        UIButton * usernameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [usernameBtn setUserInteractionEnabled:YES];
        usernameBtn.tag=indexPath.row;
        [usernameBtn   addTarget:self action:@selector(usernameBtnPressed:) forControlEvents:UIControlEventTouchUpInside];



        [noofcommentsLable setTextColor:[UIColor grayColor]];
        [noofcommentsLable setFont:[UIFont fontWithName:@"Helvetica" size:12]];

        int commentcount = 0;
        if([delegate.newaddedcommentDict objectForKey:[NSString stringWithFormat:@"%@Array",[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"id"]]])
        {
            commentcount = [[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"commentcount"]intValue]+[[delegate.newaddedcommentDict objectForKey:[NSString stringWithFormat:@"%@Array",[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"id"]]]count];
        }
        else
        {
            commentcount = [[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"commentcount"]intValue];
        }


        [noofcommentsLable setText:[NSString stringWithFormat:@"%d",commentcount]];


        UIScrollView * scroll = [[[UIScrollView alloc]init]autorelease];
//        [scroll setScrollEnabled:NO];
        [scroll setUserInteractionEnabled:YES];


        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
        [recognizer setNumberOfTapsRequired:1];
        scroll.userInteractionEnabled = YES;
        [scroll addGestureRecognizer:recognizer];

        [itemcostLable setTextAlignment:NSTextAlignmentRight];

        [bottomview setFrame:CGRectMake(0,3,300,37)];
        [itemimageview setFrame:CGRectMake(0,0,300,240)];

        CGSize  size;
        if ([[[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"photos"]objectAtIndex:0]objectForKey:@"width"]!=[NSNull null]||[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"photos"]objectAtIndex:0]objectForKey:@"height"]!=[NSNull null])
        {
        size= [self aspectScaledImageSizeForImageView:itemimageview width:[[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"photos"]objectAtIndex:0]objectForKey:@"width"]floatValue] height:[[[[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"photos"]objectAtIndex:0]objectForKey:@"height"]floatValue]];
        }
        else
        {
            size = CGSizeMake(100,100);
        }

        [itemimageview setFrame:CGRectMake((300-size.width)/2,0,size.width,size.height)];
        [scroll setFrame:CGRectMake(0,43,300,size.height)];

        //place holder image
        UIImageView * placeholderimageview = [[[UIImageView alloc]init]autorelease];
        [placeholderimageview setFrame:CGRectMake(130,0,40,size.height)];
        [placeholderimageview setImage:[UIImage imageNamed:@"57.png"]];
        [placeholderimageview setContentMode:UIViewContentModeScaleAspectFit];


//        [itemimageview setBackgroundColor:[UIColor redColor]];

        [imagenameLable setFrame:CGRectMake(40,5,160,15)];
        [usernameLable setFrame:CGRectMake(40,20,160,15)];
        [usernameBtn setFrame:CGRectMake(0,5,200,30)];
        [itemcostLable setFrame:CGRectMake(220,0,70,40)];
        [fancybtn setFrame:CGRectMake(5,size.height+48,78,25)];



        [commentBtn setFrame:CGRectMake(221,size.height+48,40,25)];
        [noofcommentsLable setFrame:CGRectMake(240,size.height+48,20,25)];
        [addrditbtn setFrame:CGRectMake(265,size.height+48,27,25)];



//        [commentBtn setFrame:CGRectMake(191,size.height+48,40,25)];
//        [noofcommentsLable setFrame:CGRectMake(211,size.height+48,20,25)];
//        [addrditbtn setFrame:CGRectMake(236,size.height+48,27,25)];
//        [cartbtn  setFrame:CGRectMake(268,size.height+48,27,25)];

        [contentview setFrame:CGRectMake(10,5,300,size.height+78)];
        [userimage setFrame:CGRectMake(5,5,30,30)];



        [scroll setBackgroundColor:[UIColor colorWithRed:0.976 green:0.976 blue:0.976 alpha:1]];

        [scroll setBackgroundColor:[UIColor clearColor]];


        contentview.clipsToBounds = NO;
        contentview.layer.masksToBounds = NO;
        contentview.layer.shadowColor = [[UIColor grayColor] CGColor];
        contentview.layer.shadowOffset = CGSizeMake(0,1);
        contentview.layer.shadowOpacity = 0.2;
        contentview.layer.shadowRadius = 0.6;
        contentview.layer.cornerRadius = 6.0; // set as you want.
        [bottomview setBackgroundColor:[UIColor clearColor]];

        userimage.layer.cornerRadius = 15.0;
        userimage.layer.masksToBounds = YES;

        [itemimageview setUserInteractionEnabled:YES];
        [scroll setBackgroundColor:[UIColor clearColor]];
        [bottomview setBackgroundColor:[UIColor clearColor]];

        [contentview setBackgroundColor:[UIColor whiteColor]];
        //place holder image

        [contentview addSubview:scroll];
        [scroll addSubview:placeholderimageview];
        [scroll addSubview:itemimageview];
        [contentview addSubview:bottomview];
        [contentview addSubview:fancybtn];
        [contentview addSubview:addrditbtn];
        [contentview addSubview:commentBtn];
        [contentview addSubview:noofcommentsLable];
        [contentview addSubview:userimage];
        [contentview addSubview:usernameLable];
        [contentview addSubview:usernameBtn];


        [cell.contentView addSubview:contentview];




    [cell setBackgroundColor:[UIColor clearColor]];
    [cell.contentView setBackgroundColor:[UIColor clearColor]];

    // Configure the cell...

    return cell;
}

【问题讨论】:

  • 滚动的平滑度是索引路径行的单元格复杂性的函数:长函数会使滚动变得更糟。更改代码以在已配置单元格时跳过设置单元格的视觉效果(即各种子视图)。添加一个标志,并在第一次设置它。然后更新视觉效果的状态,而不是更新它们。
  • 仍然没有使用 ARC——为什么?
  • recognizer 正在泄漏
  • 为您的表格视图使用自定义单元格,而不是在代码中创建它们
  • 是的,我已经更改了我的代码。现在我正在使用自定义单元格,它工作正常

标签: ios uitableview ios7 ios6.1


【解决方案1】:

这是因为在每次cellForRowAtIndexPath: 方法调用您重新创建所有子视图时。

您只能执行一次。 创建一个继承自UITableViewCell 的类。

//MyCell.h 文件

@interface MyCell : UITableViewCell

//here declare all views that you need, e.g.
@property (strong, nonatomic) UILabel *imagenameLable;
@property (strong, nonatomic) UILabel *usernameLable;
// and so on. But only those, that you mast set value (or read values) outside of this class.
// the other views declare in MyClass.m file, so they are for private use (eg. contentview,etc...) 

@end

//MyCell.m 文件

@interface MyCell()

// here declare private properties
@property (strong, nonatomic) UIView* contentview;
// and so on...

@end

@implementation

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    //and finally, in this method build your views, i.e. allocate your views, add subview of this cell, set frames, font to labels, text colors, etc...

    //e.g.
    self.contentview = [[[UIView alloc]init]autorelease];
    self.contentview.clipsToBounds = NO;
    self.contentview.layer.masksToBounds = NO;
    self.contentview.layer.shadowColor = [[UIColor grayColor] CGColor];
    self.contentview.layer.shadowOffset = CGSizeMake(0,1);
    self.contentview.layer.shadowOpacity = 0.2;
    self.contentview.layer.shadowRadius = 0.6;
    self.contentview.layer.cornerRadius = 6.0; // set as you want.

    // initialize other views

    [self.contentview addSubview:self.scroll];
    [self.scroll addSubview:self.placeholderimageview];
    [self.scroll addSubview:self.itemimageview];
    [self.contentview addSubview:self.bottomview];
    [self.contentview addSubview:self.fancybtn];
    [self.contentview addSubview:self.addrditbtn];
    [self.contentview addSubview:self.commentBtn];
    [self.contentview addSubview:self.noofcommentsLable];
    [self.contentview addSubview:self.userimage];
    [self.contentview addSubview:self.usernameLable];
    [self.contentview addSubview:self.usernameBtn];



    [self.contentView addSubview:self.contentview];
    // something like this.
}

@end 

最后

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellidentifier=@"cell";

    MyCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];

    if (cell ==nil)
    {

        cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]autorelease];

    }

    // here set values to properties of your cell. EG set text, change color if needed, change frame if needed, etc... But not remove and add them again!

    cell.usernameLable.text = [NSString stringWithFormat:@"@%@",[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"sellername"]]];
    //etc...


    return cell;
}

【讨论】:

    【解决方案2】:

    我找到了我的代码的解决方案

    这是因为

    contentview.layer.shadowOffset = CGSizeMake(0,1);
    contentview.layer.shadowOpacity = 0.2;
    contentview.layer.shadowRadius = 0.6;
    contentview.layer.cornerRadius = 6.0; // set as 
    

    一旦我从我的代码中删除了这一行,它就可以正常工作了

    【讨论】:

    • 图层操作非常昂贵。尽管这已经解决了您最初的问题,但通过查看您的代码,我很惊讶您在其他地方没有看到问题。值得阅读一些有关正确创建自定义表格视图单元格的 cmets 和答案。
    【解决方案3】:

    您的代码中有很多优化问题。您应该使用InstrumentsTime Profiler 模板来检测瓶颈。

    顺便说一句,当你绘制 CALayer 的阴影时,你应该设置 CALayer 的 shadowPath 属性作为阴影的轮廓。这将大大加快绘制阴影的速度。详情请参考documentation

    dialogContainer.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:dialogContainer.bounds cornerRadius:dialogContainer.layer.cornerRadius].CGPath;
    

    【讨论】:

      猜你喜欢
      • 2011-11-06
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 2014-02-07
      • 2019-07-20
      • 2019-10-03
      相关资源
      最近更新 更多