0

And I complete IOS app in IOS 7 and its works fine IOS 7 and then I start converting my app to IOS 6 I face lot of problems.After a huge struggle I rectify almost all the issuse other that Performance issue

I am using UITableview to display all the contents and when I test my app in Iphone 5c there is no problem in scrolling the table view. And the I test my app in ipod retina list is not scrolling smoothly. It is one of the huge problem for me and also Its killing my time

To illustrate my issue I have added my tableview cell code below. Please Suggest me some quick solution

UITableviewCell Code

- (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;
}
btmanikandan
  • 1,923
  • 2
  • 25
  • 45

3 Answers3

1

Its because in every time when cellForRowAtIndexPath: method called you recreating all subviews again.

You must do it only once. Make a class that inherits from UITableViewCell.

//MyCell.h file

@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 file

@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 

And finally

- (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;
}
arturdev
  • 10,884
  • 2
  • 39
  • 67
0

I found the solution for my code

This is because of

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

Once I remove this line from my code Its works fine

btmanikandan
  • 1,923
  • 2
  • 25
  • 45
  • Layer manipulations are very expensive. Even though this has solved your initial problem, from looking at your code I'm surprised you aren't seeing issues elsewhere. It would be worth reading some of the comments and answers regarding correct creation of custom table view cells. – CW0007007 Jul 14 '14 at 12:37
0

There is lot of optimalization issues in your code. You should use Instruments with Time Profiler template to detect bottlenecks.

BTW, When you draw shadow of CALayer you should set CALayer's shadowPath property for outline of the shadow. This will speed up drawing your shadow much. Please refer documentation for more details.

dialogContainer.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:dialogContainer.bounds cornerRadius:dialogContainer.layer.cornerRadius].CGPath;
psci
  • 903
  • 9
  • 18