Question

I have a tableview with four different types of cells. On each cell i need to place an image and labels showing with different type of text as well as label,So for better performance what i did is on scrollViewDidEndDecelerating i am trying to render the images on visible cells only. When I used reloadRowsAtIndexPaths to reload specific cell it's crashing. Showing error message as [YesNoCell compare:]: unrecognized selector sent to instance 0x1edc6a40. Any idea were i am going wrong..

Here is My code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Feed *feedObject = [self.feedList objectAtIndex:indexPath.row];

    BOOL isTableDecelarating = NO;

    if([tableView isDecelerating]){
        isTableDecelarating = YES;
    }

    switch ([feedObject.votingStyle intValue]) {
        case 4:{

            StarCell *cell =(StarCell *)[tableView dequeueReusableCellWithIdentifier:@"starCell"];

            if(!cell){

                NSLog(@"StarCell");

                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StarCell" owner:self options:nil];

                cell = (StarCell *)[nib objectAtIndex:0];
                [cell setBackgroundColor:[UIColor clearColor]];
                [cell.questionLabel setFont:[UIFont fontWithName:PROXIMA_NOVA_SEMI_BOLD size:17]];

                cell.avatarButton.layer.borderWidth = 2.0f;
            }

            cell.feedBgImage.image = nil;

              if([feedObject.feedType intValue] == 3){

                if(!isTableDecelarating)
                    cell.feedBgImage.image = [UIImage imageNamed:[@"bg_feed" concatInt:[feedObject.feedBgType intValue]+1]];

            }
            else  if([feedObject.feedType intValue] ==2){

                cell.videoButton.hidden = NO;

                UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.thumbImageURL];

                if(image){

                    [cell.loadIndicator stopAnimating];
                    if(!isTableDecelarating)
                        cell.feedBgImage.image =image;
                }
                else{
                    [self downloadImageFor:feedObject.thumbImageURL atIndex:indexPath.row];
                }


            }

            else{

                UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.resourceId];

                if(image){

                    [cell.loadIndicator stopAnimating];
                    if(!isTableDecelarating)
                        cell.feedBgImage.image =image;
                }
                else{
                    [self downloadImageFor:feedObject.resourceId atIndex:indexPath.row];
                }
            }



            return cell;


            break;

        }

        case 2:{

            YesNoCell *cell =(YesNoCell *)[tableView dequeueReusableCellWithIdentifier:@"yesNoCell"];

            if(!cell){

                NSLog(@"YesNoCell");


                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YesNoCell" owner:self options:nil];

                cell = (YesNoCell *)[nib objectAtIndex:0];
                [cell setBackgroundColor:[UIColor clearColor]];
                [cell.questionLabel setFont:[UIFont fontWithName:PROXIMA_NOVA_SEMI_BOLD size:17]];
                cell.avatarImage.layer.borderWidth = 2.0f;


            }

            cell.feedBgImage.image = nil;


            if([feedObject.feedType intValue] == 3){

                [cell.loadIndicator stopAnimating];
                if(!isTableDecelarating)
                    cell.feedBgImage.image = [UIImage imageNamed:[@"bg_feed" concatInt:[feedObject.feedBgType intValue]+1]];
            }
            else  if([feedObject.feedType intValue] ==2){

                cell.videoButton.hidden = NO;

            UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.thumbImageURL];

                if(image){

                    [cell.loadIndicator stopAnimating];
                    if(!isTableDecelarating)
                        cell.feedBgImage.image =image;
                }
                else{
                    [self downloadImageFor:feedObject.thumbImageURL atIndex:indexPath.row];
            }


            }
            else{

                UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.resourceId];

                if(image){

                    [cell.loadIndicator stopAnimating];
                    if(!isTableDecelarating)
                        cell.feedBgImage.image =image;
                }
                else{
                    [self downloadImageFor:feedObject.resourceId atIndex:indexPath.row];
                }

            }



            return cell;

            break;

        }

        case 3:{

            YNMayBeCell *cell =(YNMayBeCell *)[tableView dequeueReusableCellWithIdentifier:@"ynmCell"];

            if(!cell){

                NSLog(@"YNMayBeCell");


                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YNMayBeCell" owner:self options:nil];

                cell = (YNMayBeCell *)[nib objectAtIndex:0];
                [cell setBackgroundColor:[UIColor clearColor]];
                [cell.questionLabel setFont:[UIFont fontWithName:PROXIMA_NOVA_SEMI_BOLD size:17]];

                cell.avatarButton.layer.borderWidth = 2.0f;


            }

            cell.feedBgImage.image = nil;


            if([feedObject.feedType intValue] == 3){

                if(!isTableDecelarating)
                    cell.feedBgImage.image = [UIImage imageNamed:[@"bg_feed" concatInt:[feedObject.feedBgType intValue]+1]];

            }
            else  if([feedObject.feedType intValue] ==2){

                cell.videoButton.hidden = NO;

                UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.thumbImageURL];

                if(image){

                    [cell.loadIndicator stopAnimating];
                    if(!isTableDecelarating)
                        cell.feedBgImage.image =image;
                }
                else{
                    [self downloadImageFor:feedObject.thumbImageURL atIndex:indexPath.row];
                }


            }

            else{

                UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.resourceId];

                if(image){

                    [cell.loadIndicator stopAnimating];
                    if(!isTableDecelarating)
                        cell.feedBgImage.image =image;
                }
                else{
                    [self downloadImageFor:feedObject.resourceId atIndex:indexPath.row];
                }

            }



            return cell;


            break;
        }

        default:{

            ThumbsUpCell *cell =(ThumbsUpCell *)[tableView dequeueReusableCellWithIdentifier:@"thumbsUpCell"];

            if(!cell){

                NSLog(@"ThumbsUpCell");

                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ThumbsUpCell" owner:self options:nil];

                cell = (ThumbsUpCell *)[nib objectAtIndex:0];
                [cell setBackgroundColor:[UIColor clearColor]];
                [cell.questionLabel setFont:[UIFont fontWithName:PROXIMA_NOVA_SEMI_BOLD size:17]];

                cell.avatarButton.layer.borderWidth = 2.0f;


            }

            cell.feedBgImage.image = nil;

            [cell.loadIndicator startAnimating];

            [cell.loadIndicator setHidesWhenStopped:YES];



            if([feedObject.feedType intValue] == 3){

                [cell.loadIndicator stopAnimating];
                if(!isTableDecelarating)
                    cell.feedBgImage.image = [UIImage imageNamed:[@"bg_feed" concatInt:[feedObject.feedBgType intValue]+1]];

            }
            else  if([feedObject.feedType intValue] ==2){

                cell.videoButton.hidden = NO;

                UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.thumbImageURL];

                if(image){

                    [cell.loadIndicator stopAnimating];
                    if(!isTableDecelarating)
                        cell.feedBgImage.image =image;
                }
                else{
                    [self downloadImageFor:feedObject.thumbImageURL atIndex:indexPath.row];
                }


            }

            else{

                UIImage *image = [[WSImageDownloader sharedInstance] cachedImageForRequest:feedObject.resourceId];

                if(image){

                    [cell.loadIndicator stopAnimating];
                    if(!isTableDecelarating)
                        cell.feedBgImage.image =image;
                }
                else{
                    [self downloadImageFor:feedObject.resourceId atIndex:indexPath.row];
                }

            }


            return cell;

            break;

        }
    }
}

Scrollview:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView
{

      NSArray *visibleRows = [self.homeTableView visibleCells];

   [self.homeTableView beginUpdates];

     [self.homeTableView reloadRowsAtIndexPaths:visibleRows withRowAnimation:UITableViewRowAnimationNone];

   [self.homeTableView endUpdates];

}
Was it helpful?

Solution

That's because reloadRowsAtIndexPaths: expects an array of indexPaths, but you are providing a list of table view cell objects.

Just change the line

  NSArray *visibleRows = [self.homeTableView visibleCells];

into

  NSArray *visibleRows = [self.homeTableView indexPathsForVisibleRows];

OTHER TIPS

I was stuck in this problem for 1 and half day.
Here is my code.

-(void)ShowtableHeight:(NSIndexPath*)indexpath
{
    NSArray *visibleRows = [recTableView indexPathsForVisibleRows];
    [recTableView reloadRowsAtIndexPaths:visibleRows  withRowAnimation:UITableViewRowAnimationNone];
} 

I called this function from

- (void)webViewDidFinishLoad:(UIWebView *)aWebView 

My tableView Rows Were displaying data in webView.

So for dynamic height of data I wrote ShowtableHeight function. And the indexpath parameter for ShowtableHeight function come from webViewDidFinishLoad function like this,

NSIndexPath *index = [recTableView indexPathForCell:cell];

And if you want to pass section as parameter then you can write following in webViewDidFinishLoad accordingly

NSString *key = [dateArr objectAtIndex:index.section];

This will pass the number of sections in tableview.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top