使用调度队列在 uitableview 中平滑滚动,在数组中给出随机值
Posted
技术标签:
【中文标题】使用调度队列在 uitableview 中平滑滚动,在数组中给出随机值【英文标题】:Using dispatch queue for smooth scrolling in uitableview giving shuffled values in array 【发布时间】:2013-04-09 13:39:50 【问题描述】:我正在墙上显示帖子,等等,不喜欢按钮必须显示喜欢和不喜欢的数量。帖子的数据同时来自不同的 API。但是喜欢和不喜欢的值来自不同的 API。并且对于每个帖子的不喜欢都有一个 id,该 id 被馈送到 API 然后点击。因此,对于每个帖子,此 API 被点击意味着等于单元格的数量。所以我使用了调度队列,但它在数组中显示了洗牌的值,并且没有调度队列,它给出了正确的值,但是表格在滚动时被暂停并且非常慢。如何解决这个问题,请指导。
这里是代码。
-(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];
);
);
也尝试了调度队列和异步 json,但没有得到正确的结果。 以上请多多指教。
【问题讨论】:
【参考方案1】:-在你的 .h 文件中声明变量为int serialIndexVal;
- 维护一个将跟踪 indexVal 的 serialIndexVal...
serialIndexVal++;
indexVal=serialIndexVal;
将下面的代码放入 GCD 块中
__block NSString *status = [NSString stringWithFormat:@"get"];
[self.mButtonsStatusDict setObject:status forKey:@"status"];
[self.mButtonsPidDict setObject:[self.mArrPid objectAtIndex:indexVal] forKey:@"pid"];
-On viewWillDisappear 让你的serialIndexVal=0
;再次。
【讨论】:
谢谢,但它没有给出预期的结果。 仍然在洗牌? 尝试使用 NSOperationQueue,因为它很难维持 GCD 的执行。***.com/questions/14444041/… 好的,但是我发现了一个关于这个问题的有趣事实,indexVal 的值来自 willDisplayCell 方法,对于获取数组中最多两个值,然后在数组 indexval 的索引 2 处更改为 1并继续。 对,从一开始就有 indexVal 的问题……所以说要把代码放在 GCD 中,无论如何可能发生的是当 GCD 执行一个或其他块时,来自“willDisplayCell”的 indexVal 发生变化和混乱发生...以上是关于使用调度队列在 uitableview 中平滑滚动,在数组中给出随机值的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 令人敬畏的优化AVPlayer,可在UICollectionView / UITableView中平滑滚动AVPlayerLayer(在iOS10 +上测试)
c_cpp 令人敬畏的优化AVPlayer,可在UICollectionView / UITableView中平滑滚动AVPlayerLayer(在iOS10 +上测试)