现在我正在使用uitableview,其中包含在uibutton上设置的用户图像,在mpmovieplayercontroller上录制的视频播放就像藤蔓应用程序一样。
这里我的问题是当从一行到另一行时,uitableview不能平滑滚动。
我不知道为什么会这样。任何人都知道原因为如何解决这类问题提供了一些指导。
我在这里从网络服务获取数据。
-(void)feedwebservice
{
NSString *myurl1=[NSString stringWithFormat:@"http://demo.xyz.com/client/vine-clone/feed/home_feed?user_id=%@&start=%d",AppDelegate.check,navigate];
NSString *theurl1=[myurl1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *theRequest1=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:theurl1]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSHTTPURLResponse *response;
NSError* error12;
NSData *data1=[NSURLConnection sendSynchronousRequest:theRequest1 returningResponse:&response error:&error12];
NSString *strResponse1=[[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding];
NSMutableString* responseString = [NSMutableString stringWithString:strResponse1];
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *dic1 = [parser objectWithString:responseString error:nil];
for(NSDictionary *subDic in dic1)
{
obj=[[DataSet alloc]init];
obj.data1=[subDic objectForKey:@"channel"];
obj.data2=[subDic objectForKey:@"followers"];
obj.data3=[subDic objectForKey:@"following"];
obj.data4=[subDic objectForKey:@"fullname"];
obj.data5=[subDic objectForKey:@"post_private"];
obj.data6=[subDic objectForKey:@"post_sensitive"];
obj.data7=[subDic objectForKey:@"profile_image"];
obj.data8=[subDic objectForKey:@"status"];
obj.data9=[subDic objectForKey:@"title"];
obj.data10=[subDic objectForKey:@"total_like"];
obj.data11=[subDic objectForKey:@"total_vine"];
obj.data12=[subDic objectForKey:@"user_id"];
obj.data13=[subDic objectForKey:@"user_like"];
obj.data14=[subDic objectForKey:@"user_vine"];
obj.data15=[subDic objectForKey:@"video_created_date"];
obj.data16=[subDic objectForKey:@"video_id"];
obj.data17=[subDic objectForKey:@"video_name"];
[feedarr addObject:obj];
}
//NSLog(@"feedpage count = %u",[feedarr count]);
[videotable reloadData];
[pullToRefreshManager_ tableViewReloadFinished];
}
uitableview单元格用于索引路径行的数据
-(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];
obj=[feedarr objectAtIndex:indexPath.row];
profileimgbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[profileimgbtn setFrame:CGRectMake(10, 280, 50, 40)];
[profileimgbtn setBackgroundColor:[UIColor clearColor]];
[profileimgbtn setBackgroundImage:[UIImage imageNamed:@"Placeholder.png"] forState:UIControlStateNormal];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
obj.data7 = [obj.data7 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *imageURL = [NSURL URLWithString:obj.data7];
NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
UIImage *image = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
[profileimgbtn setBackgroundImage:image forState:UIControlStateNormal];
});
});
[cell addSubview:profileimgbtn];
playerview = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 250)];
[playerview setBackgroundColor:[UIColor orangeColor]];
[cell addSubview:playerview];
obj.data17 = [obj.data17 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *contentURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",obj.data17]];
mc = [[MPMoviePlayerController alloc] initWithContentURL:contentURL];
mc.shouldAutoplay = YES;
mc.controlStyle = MPMovieControlStyleNone;
mc.contentURL = contentURL;
[mc prepareToPlay];
mc.repeatMode=MPMovieRepeatModeOne;
mc.view.frame = playerview.bounds;
[mc setScalingMode:MPMovieScalingModeFill];
[mc play];
[playerview addSubview:mc.view];
//Get the image from the currentPlaybackTime to set it above the player
thumbnail = [mc thumbnailImageAtTime:[mc currentPlaybackTime] timeOption:MPMovieTimeOptionNearestKeyFrame];
thumbnailView = [[UIImageView alloc] initWithFrame:mc.view.frame];
[thumbnailView setImage:thumbnail];
[thumbnailView setContentMode:UIViewContentModeScaleToFill];
thumbnailView.clipsToBounds = YES;
[playerview addSubview:thumbnailView];
singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleRollTap:)];
[singleFingerTap setDelegate:self];
singleFingerTap.numberOfTouchesRequired=1;
singleFingerTap.numberOfTapsRequired=1;
[mc.view addGestureRecognizer:singleFingerTap];
self.moviePlayerController = mc;
playerActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
playerActivityIndicator.frame = CGRectMake(140, 110, 25, 25);
[playerActivityIndicator startAnimating];
[playerview addSubview:playerActivityIndicator];
[self performSelector:@selector(spinnerhide) withObject:nil afterDelay:5.0];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
答案 0 :(得分:2)
您似乎已经注释掉了单元格重用 - 这是一个惊人的性能保存功能,用于限制创建的单元格数量,从而限制系统征税的数量。
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
obj=[feedarr objectAtIndex:indexPath.row];
...
答案 1 :(得分:2)
您可以从互联网添加EgoImagebuton
文件:https://github.com/enormego/EGOImageLoading
然后,使用egoimagebuton
代替UIImageView
。
答案 2 :(得分:1)
这真的很糟糕,你不应该每次都创建单元格:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
/*if(cell == nil)
{
}*/
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
应该是:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
答案 3 :(得分:0)
除了按照其他答案中的建议重新使用单元格之外,还需要确保在完成处理程序中,通过向表视图发送返回单元格的相应消息,将再次显式获取分配图像的单元格。基于 indexPath 。
您需要这样做的原因是,一旦您重复使用单元格,并且一旦执行完成处理程序,该单元格就不再像以前那样与indexPath相关联。
因此,在您的完成处理程序中,根据indexPath重新获取单元格:
dispatch_async(dispatch_get_main_queue(), ^{
MyTableViewCell* cell = (MyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell.addProfileImagegButton setBackgroundImage:image forState:UIControlStateNormal];
});
在这里,假设您已将UITableViewCell
子类化为属性profileImagegButton
。
此外,如果发生该单元格不再可见,cellForRowAtIndexPath:
将返回nil
。
但是,完整的解决方案要求您实现图像缓存,并且当当前存在具有相同URL挂起的请求时,还要阻止加载URL。在设置占位符图像之前,您需要检查缓存是否有图像。