IOS UITableview Scroll在IOS6中并不流畅

时间:2014-07-12 13:20:30

标签: ios uitableview ios7 ios6.1

我在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;
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

我找到了代码的解决方案

这是因为

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

从代码中删除此行后,其工作正常

答案 2 :(得分:0)

您的代码中存在大量优化问题。您应该使用InstrumentsTime Profiler模板来检测瓶颈。

顺便说一句,当您绘制CALayer的阴影时,您应该设置CALayer的shadowPath属性以获得阴影轮廓。这将加快你的阴影。有关详细信息,请参阅documentation

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