Reputation: 1395
My tableview lags when I scroll it. I have used custom cell because there are number of contents view in a cell. But when I scroll the tableview It doesn't scroll smoothly. I am not getting whats the issue in my coding please help me out to find the issue. Here is my code:
static NSString *CellIdentifier = @"Cell1";
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor=[UIColor blackColor];
cell.frame = CGRectMake(0.0, 0.0, 320.0, 200.0);
UITapGestureRecognizer *tap1 =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefirstTap:)];
tap1.delegate=self;
tap1.numberOfTapsRequired=1;
[cell.image1 addGestureRecognizer:tap1];
[tap1 release];
UITapGestureRecognizer *tap2 =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesecondTap:)];
tap2.delegate=self;
tap2.numberOfTapsRequired=1;
[cell.image2 addGestureRecognizer:tap2];
[tap2 release];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.image1 setTag:indexPath.row*2];
[cell.image2 setTag:indexPath.row*2+1];
NSString *checkfirstcat=[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"category"];
NSString *checksecondcat=[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"category"];
for (int i = 0; i < [categories count]; i++) {
if ([checkfirstcat isEqualToString:[categories objectAtIndex:i]]) {
cell.first.backgroundColor=[uniquecolors objectAtIndex:i];
cell.veryfirst.backgroundColor=[uniquecolors objectAtIndex:i];
break;
}
}
for (int i = 0; i < [categories count]; i++) {
if ([checksecondcat isEqualToString:[categories objectAtIndex:i]]) {
cell.second.backgroundColor=[uniquecolors objectAtIndex:i];
cell.verysecond.backgroundColor=[uniquecolors objectAtIndex:i];
break;
}
}
cell.veryfirst.text= [NSString stringWithFormat:@" %@",[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"rank"]];
cell.first.text=[NSString stringWithFormat:@" %@",[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"category"]];
cell.verysecond.text=[NSString stringWithFormat:@" %@", [[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"rank"]];
cell.second.text=[NSString stringWithFormat:@" %@",[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"category"]];
UIActivityIndicatorView *spinner1 =[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinner1.frame = CGRectMake(55, 55, 40, 40);
[cell.contentView addSubview:spinner1];
[spinner1 startAnimating];
UIActivityIndicatorView *spinner2 = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinner2.frame = CGRectMake(220, 55, 40, 40);
[cell.contentView addSubview:spinner2];
[spinner2 startAnimating];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0ul);
dispatch_async(queue, ^{
NSString *pathimage1 = [[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"image"];
NSString *pathimage2 = [[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"image"];
dispatch_sync(dispatch_get_main_queue(), ^{
[cell.image1 setImageWithURL:[NSURL URLWithString:pathimage1] placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(UIImage *image, BOOL cached){
[spinner1 stopAnimating];
} failure:^(NSError *error) {
[spinner1 stopAnimating];
}];
[cell.image2 setImageWithURL:[NSURL URLWithString:pathimage2] success:^(UIImage *image, BOOL cached) {
[spinner2 stopAnimating];
} failure:^(NSError *error) {
[spinner2 stopAnimating];
}];
});
});
cell.btn1.tag=indexPath.row*2;
cell.btn2.tag=indexPath.row*2+1;
[cell.btn1 setTitle:[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"total_like"] forState:UIControlStateNormal];
[cell.btn2 setTitle:[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"total_like"] forState:UIControlStateNormal];
NSString *btn1status=[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"like_stat"];
NSString *btn2status=[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"like_stat"];
if (![btn1status isEqualToString:@"false"]) {
[cell.btn1 setImage:[UIImage imageNamed: @"fav-icon.png"] forState:UIControlStateNormal];
}
else
{
[cell.btn1 setImage:[UIImage imageNamed: @"fav.png"] forState:UIControlStateNormal];
}
if (![btn2status isEqualToString:@"false"]) {
[cell.btn2 setImage:[UIImage imageNamed: @"fav-icon.png"] forState:UIControlStateNormal];
}
else
{
[cell.btn2 setImage:[UIImage imageNamed: @"fav.png"] forState:UIControlStateNormal];
}
[cell.btn1 addTarget:self action:@selector(like1:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn2 addTarget:self action:@selector(like2:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
Upvotes: 2
Views: 675
Reputation: 1807
As NSAddict points out it is not recommended to do all these in the tableView:cellForRowAtIndexPath:
Also you are performing 2 iterations when it appears as if only one is needed.
for(int i = 0; i < [categories count]; i++) {}
It appears that you are using SDWebImage or some kind of async image loader library for images. If u are using SDWebImage the recommended way is to use a category(for UIactivityIndicator) as per its documentation. See this;https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage
Upvotes: 1
Reputation: 6862
I guess you're doing all this in the tableView:cellForRowAtIndexPath:
method? This is usually bad. Especially creating stuff like UIActivityIndicatorView
views etc.
Remember that this method is called every time a row appears on the screen. Even if you scroll down, and then up again. So this method will be called for every row, many times.
If you want to add a spinner, it's much smarter to configure this in Interface Builder, and then only call the spinner to animate.
Upvotes: 2