1

I am showing post on wall, and the like, dislike buttons that have to show the number of likes and dislikes on it. The data of post is coming from different API at once. But the values of like and dislike is coming from different API. And for like dislikes of each post has an id which is fed to API and then hit. So, for each post this API is being hit means equal to the number of cells. So i used dispatch queue but it is showing shuffled values in array, and without dispatch queue, it is giving correct values but table gets paused and very slow while scrolling. How to solve this issue, please guide.

Here is the code.

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

Cell *cell = (Cell*)[self.mPublicFriendTable dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
    cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else{
    cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"text_boxbg.png"]];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.mLblName.frame = CGRectMake(50, 9, 150, 18);
cell.mLblTxt.frame = CGRectMake(50, 26, 218, 29);

if (indexPath.section == [self.mArrLike count])
{
     cell.textLabel.text = @"Load More...";
} else
{
strLk = [self.mArrLike objectAtIndex:indexPath.section];
strDLk = [self.mArrDislike objectAtIndex:indexPath.section];

   // NSLog(@"mARRUname:%d", [self.mArrUName count]);
cell.mLblName.text = [self.mArrUName objectAtIndex:indexPath.section];
CGSize size = [cell.mLblName.text sizeWithFont:[UIFont boldSystemFontOfSize:20.0]];
cell.mLblImg.frame = CGRectMake(size.width+20,14, 9, 14);

NSString *imageUrlString = [self.mArrProfileImg objectAtIndex:indexPath.section];
NSLog(@"counter at:%d",indexPath.section);
asyncImageView = [[AsyncImageView alloc]initWithFrame:CGRectMake(9, 9, 37, 37)];
[asyncImageView loadImageFromURL:[NSURL URLWithString:imageUrlString]];
[cell.contentView addSubview:asyncImageView];

cell.mLblTxt.text = [self.mArrText objectAtIndex:indexPath.section];
cell.mLblDate.text = [self.mArrDate objectAtIndex:indexPath.section];
cell.mLblTimeImg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"message.png"]];
cell.mLblDate.textColor = [UIColor colorWithRed:109.0/255.0 green:218.0/255.0 blue:250.0/255.0 alpha:1.0];

[cell.mBtnLike setImage:[UIImage imageNamed:@"thumb_up.png"] forState:UIControlStateNormal];
    [cell.mBtnLike addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
//[cell.mBtnLike setBackgroundColor:[UIColor colorWithRed:47.0/255.0 green:200.0/255.0 blue:247.0/255.0 alpha:1.0]];
[cell.mBtnLike setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -10)];
cell.mBtnLike.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
[cell.mBtnLike setTitle:strLk forState:UIControlStateNormal];

[cell.mBtnDislike setImage:[UIImage imageNamed:@"thumb_down.png"] forState:UIControlStateNormal];
[cell.mBtnDislike addTarget:self action:@selector(dislike) forControlEvents:UIControlEventTouchUpInside];
 //[cell.mBtnDislike setBackgroundColor:[UIColor colorWithRed:47.0/255.0 green:200.0/255.0 blue:247.0/255.0 alpha:1.0]];
[cell.mBtnDislike setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -10)];
cell.mBtnDislike.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
[cell.mBtnDislike setTitle:strDLk forState:UIControlStateNormal];

[cell.mBtnShare setImage:[UIImage imageNamed:@"back_icon.png"] forState:UIControlStateNormal];
[cell.mBtnShare addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
 //[cell.mBtnShare setBackgroundColor:[UIColor colorWithRed:47.0/255.0 green:200.0/255.0 blue:247.0/255.0 alpha:1.0]];
cell.mBtnShare.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -35);
[cell.mBtnShare setTitleEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];
[cell.mBtnShare setTitle:@"" forState:UIControlStateNormal];

[cell.mBtnComm setImage:[UIImage imageNamed:@"chat_icon.png"] forState:UIControlStateNormal];
[cell.mBtnComm addTarget:self action:@selector(comments) forControlEvents:UIControlEventTouchUpInside];
//[cell.mBtnComm setBackgroundColor:[UIColor colorWithRed:47.0/255.0 green:200.0/255.0 blue:247.0/255.0 alpha:1.0]];
[cell.mBtnComm setTitleEdgeInsets:UIEdgeInsetsMake(0, -52, 0, 0)];
cell.mBtnComm.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -50);
[cell.mBtnComm setTitle:@"30" forState:UIControlStateNormal];
}
return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"count like:%d", [self.mArrLike count]);
NSLog(@"count uname:%d", [self.mArrUName count]);
if(indexPath.section == ([self.mArrLike count]-1))
{
[self loadData:indexPath.section];
}
}

-(void)loadData:(int)indexVal;
{
status = [NSString stringWithFormat:@"get"];
[self.mButtonsStatusDict setObject:status forKey:@"status"];

[self.mButtonsPidDict setObject:[self.mArrPid objectAtIndex:indexVal] forKey:@"pid"];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{

    self.mButtonsDict = [MyEventApi likeDislike:self.mButtonsUidDict post:self.mButtonsPidDict postStatus:self.mButtonsStatusDict];
    NSLog(@"buttons data dict:%@", self.mButtonsDict);


    dispatch_sync(dispatch_get_main_queue(), ^{

        [self.mArrLike addObject: [self.mButtonsDict valueForKey:@"like"]];
        [self.mArrDislike addObject: [self.mButtonsDict valueForKey:@"dislike"]];

        NSLog(@"arrLike count:%d", [self.mArrLike count]);
       // strLk = [self.mArrLike objectAtIndex:indexPath.section];
       // strDLk = [self.mArrDislike objectAtIndex:indexPath.section];

        [self.mPublicFriendTable reloadData];
    });
});
}

Tried with dispatch queue and asynchronous json also, but doesn't get proper results. Please guide for the above thanks in advance.

enter image description here

1 Answers1

0

-Declare Variable inside your .h file as int serialIndexVal;

-maintain a serialIndexVal which will keep track of indexVal...

serialIndexVal++; 
indexVal=serialIndexVal; 

Put below code inside GCD block

__block NSString *status = [NSString stringWithFormat:@"get"];
 [self.mButtonsStatusDict setObject:status forKey:@"status"]; 
[self.mButtonsPidDict setObject:[self.mArrPid objectAtIndex:indexVal] forKey:@"pid"];

-On viewWillDisappear make your serialIndexVal=0; again.

BhushanVU
  • 3,455
  • 1
  • 24
  • 33
  • Thanks, but it is not giving the desired results. –  Apr 10 '13 at 05:32
  • still getting shuffled cells? – BhushanVU Apr 10 '13 at 05:38
  • try using NSOperationQueue as its difficult to maintain GCD's execution.. http://stackoverflow.com/questions/14444041/nsoperationqueue-background-download-images – BhushanVU Apr 10 '13 at 06:56
  • Ok, but i found an interesting fact about the problem, the indexVal whose value coming from the willDisplayCell method, remains constant for fetching upto two values in array, then at index 2 of array indexval change to 1 and continues. –  Apr 10 '13 at 07:11
  • right, problem was with indexVal from beginning...so said to put code inside GCD, anyways what could be happening is when GCD executing one or other block, indexVal coming from 'willDisplayCell' changes and mess happens... – BhushanVU Apr 10 '13 at 07:16
  • maintain a Variable which will keep track of indexVal... static int serialIndexVal; serialIndexVal++; indexVal=serialIndexVal; Put below code inside GCD block __block NSString *status = [NSString stringWithFormat:@"get"]; [self.mButtonsStatusDict setObject:status forKey:@"status"]; [self.mButtonsPidDict setObject:[self.mArrPid objectAtIndex:indexVal] forKey:@"pid"]; – BhushanVU Apr 10 '13 at 07:28
  • how to link here serialIndexVal with indexVal? –  Apr 10 '13 at 08:00
  • if you want to do serial execution of GCD then easy way is to maintain an variable..above approach i suggested is just a shot... – BhushanVU Apr 10 '13 at 08:02
  • Hey, it works thanks, but another small issue comes, that when viewDidLoad is called again the loadData method still holding the indexVal value and thus index goes beyond bound and it kills, how come it possible when a view controller is called again everything is re-initialized and the tableview is called later after calling this method , array's are filled then tableview populates, then how it is holding previous index value? –  Apr 10 '13 at 09:30
  • gr8..check updated answer...this is because static variable we used..change it to normal one... – BhushanVU Apr 10 '13 at 09:40
  • Ya, that was done, i was doing segment control, there is two types of wall that i am displaying public and friend, but that thing was done. Thanks a lot for that, i was stuck since two days almost, but now done, thanks. –  Apr 10 '13 at 10:39
  • glad to help...and would appreciate if above edited answer got upvoted :) – BhushanVU Apr 10 '13 at 10:46