我有按钮的自定义单元格。点击按钮我从服务器下载视频。在下载过程中,我需要显示活动指示符,但它不会显示activityindicatorview
。
代码:
RBFriendChatTableViewCell *Cell = [ tableView dequeueReusableCellWithIdentifier:Identifier];
[Cell.m_btnDownLoad addTarget:self action:@selector(downLoadImage:) forControlEvents:UIControlEventTouchUpInside];
-(void)downLoadImage:(UIbutton *)sender{
indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.center = CGPointMake(5, 5);
[sender addSubview:indicator];
[indicator startAnimating]
NSString *strUrl = [NSString stringWithFormat:@"%@%@",chatImage,[dicChatList objectForKey:@"file"]];
NSURL *url=[[NSURL alloc] initWithString:strUrl];
NSData *movieData = [NSData dataWithContentsOfURL:url];
NSURL *fileURL = [self grabFileURL:[dicChatList objectForKey:@"file"]];
[movieData writeToURL:fileURL atomically:YES];
[m_arrDownloadVideoName addObject:[dicChatList objectForKey:@"file"]];
}
答案 0 :(得分:0)
你需要在主线程中更新UI,你正在同一个线程中下载视频,它应该在后台Asynch线程中完成,对于你当前的情况,修复后可能会有所帮助,但最好在后台进行下载并在完成后更新UI。
-(void)downLoadImage:(UIbutton *)sender{
dispatch_async(dispatch_get_main_queue(), ^{
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle : UIActivityIndicatorViewStyleGray];
indicator.center = CGPointMake(5, 5);
[sender addSubview:indicator];
[indicator startAnimating];
});
NSString *strUrl = [NSString stringWithFormat:@"%@%@",chatImage,[dicChatList objectForKey:@"file"]];
NSURL *url=[[NSURL alloc] initWithString:strUrl];
NSData *movieData = [NSData dataWithContentsOfURL:url];
NSURL *fileURL = [self grabFileURL:[dicChatList objectForKey:@"file"]];
[movieData writeToURL:fileURL atomically:YES];
[m_arrDownloadVideoName addObject:[dicChatList objectForKey:@"file"]];
}