Comments (9)
我这里是自定义cell
在SDFileDownloadView.m的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中实现:
-
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SDFileTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];[self cellDidSelectRow:cell];
}
-
(void) cellDidSelectRow :(SDFileTableViewCell *)cell {
// 1.0先隐藏掉下载按钮
[cell.selectButton setHidden:YES];
// 1.1显示下载进度label
[cell.downloadSpeedLabel setHidden:NO];// 2.1 下载文件url
NSArray<NSString *> *urls = @[@"http://teaching.csse.uwa.edu.au/units/CITS4401/practicals/James1_files/SPMP1.pdf", @"http://down.51rc.com/dwndoc/WrittenExamination/WrittenExperiences/dwn00006795.doc", @"http://video1.remindchat.com/20190905/1gEji0Sv/mp4/1gEji0Sv.mp4", @"https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4", @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4", @"http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4", ];
__weak typeof(self)weakSelf = self;
[urls enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
MJDownloadInfo *info = [[MJDownloadManager defaultManager] downloadInfoForURL:obj]; if (info.state == MJDownloadStateCompleted) { // 下载完成之后,下载中图标更换为下载完成图标 [cell.downloadSpeedLabel setHidden:YES]; [cell.selectButton setHidden:NO]; [cell.selectButton setImage:[UIImage imageNamed:@"downloadok"] forState:UIControlStateNormal]; }else { // 未下载状态 NSLog(@"222222222222"); } if (info.state == MJDownloadStateResumed || info.state == MJDownloadStateWillResume) { } else if (info.state == MJDownloadStateSuspened || info.state == MJDownloadStateNone) { [[MJDownloadManager defaultManager] download:obj progress:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { dispatch_async(dispatch_get_main_queue(), ^{ // 2.2 下载中进度显示 cell.downloadSpeedLabel.text = [NSString stringWithFormat:@"%.2f%%", (CGFloat)totalBytesWritten / totalBytesExpectedToWrite * 100.0]; }); } state:^(MJDownloadState state, NSString *file, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ // 2.3 下载完成,改变按钮状态,保存下载文件路径,保存cell点击状态 if (state == MJDownloadStateCompleted) { [cell.downloadSpeedLabel setHidden:YES]; [cell.selectButton setHidden:NO]; [cell.selectButton setImage:[UIImage imageNamed:@"downloadok"] forState:UIControlStateNormal]; NSLog(@"打印保存文件路径地址 :%@",file); } }); }]; }else if (info.state == MJDownloadStateCompleted) { // 已下载完成的文件取url链接跳转阅读控制器 NSString * filepath = [NSString stringWithFormat:@"%@",info.file]; if ([self.delegate respondsToSelector:@selector(cellFilePathStr:)]) { // 移除下载列表视图 [self removeFromSuperview]; // 代理方法跳转阅读文档控制器 [self.delegate cellFilePathStr:filepath]; } }
}];
}
发现点击一行cell时,可以正常实现下载文件,但是当我想实现多点击几行cell让它多文件同时下载的时候,不知道应该怎么去实现,希望得到您的帮助
from iosproject.
考虑到cell的复用问题; 建议您这里修改下, 优先更新模型数据, 再更新UI数据
from iosproject.
抱歉, 注释写的不是很清楚, 补了些注释, 看下还有什么疑问吗?
// 数据
NSArray<NSString *> *urls = @[
@"http://teaching.csse.uwa.edu.au/units/CITS4401/practicals/James1_files/SPMP1.pdf",
@"http://down.51rc.com/dwndoc/WrittenExamination/WrittenExperiences/dwn00006795.doc",
@"http://video1.remindchat.com/20190905/1gEji0Sv/mp4/1gEji0Sv.mp4",
@"https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4",
@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
@"http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4",
];
self.title = @"点击Cell开始/暂停下载";
LMJWeak(self);
// 遍历URL个数创建对应的模型数组
[urls enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 获取下载文件对象
MJDownloadInfo *info = [[MJDownloadManager defaultManager] downloadInfoForURL:obj];
NSString *subTitle = nil;
// 比对缓存是否下载完毕
if (info.state == MJDownloadStateCompleted) {
subTitle = @"播放";
}else {
// 比对缓存进度
CGFloat progress = ((CGFloat)info.totalBytesWritten) / info.totalBytesExpectedToWrite * 100;
subTitle = [NSString stringWithFormat:@"进度: %.2f%%, 点击开始", isnan(progress) ? 0 : progress];
}
// 添加数据模型, 和 绑定点击事件;
// 考虑到cell的复用问题, 这个cell点击的时候, 调用了模型的itemOperation回调,
self.addItem([LMJWordItem itemWithTitle:[obj.lastPathComponent substringToIndex:5] subTitle:subTitle itemOperation:^(NSIndexPath *indexPath) {
// 文件下载状态: 下载中和在下载队列排队, 最大3个下载
if (info.state == MJDownloadStateResumed || info.state == MJDownloadStateWillResume) {
// 暂停
[[MJDownloadManager defaultManager] suspend:info.url];
// 获取进度
CGFloat progress = ((CGFloat)info.totalBytesWritten) / info.totalBytesExpectedToWrite * 100;
// 刷新模型
weakself.sections.firstObject.items[indexPath.row].subTitle = [NSString stringWithFormat:@"暂停中..进度: %.2f%%", isnan(progress) ? 0 : progress];
// 刷新UI, 获取不到就不刷新UI, 下次滚动cell赋值模型的时候, 还会刷新模型数据
((LMJSettingCell *)[weakself.tableView cellForRowAtIndexPath:indexPath]).item = weakself.sections.firstObject.items[indexPath.row];
} else if (info.state == MJDownloadStateSuspened || info.state == MJDownloadStateNone) { // 暂停中和无状态; 开始下载
// 开始下载obj = Url
[[MJDownloadManager defaultManager] download:obj progress:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
dispatch_async(dispatch_get_main_queue(), ^{
// 更新模型
weakself.sections.firstObject.items[indexPath.row].subTitle = [NSString stringWithFormat:@"进度: %.2f%%", (CGFloat)totalBytesWritten / totalBytesExpectedToWrite * 100.0];
// 更新视图; 获取不到cell就不刷新UI, 下次滚动cell然后赋值模型的时候, 还会刷新模型数据
((LMJSettingCell *)[weakself.tableView cellForRowAtIndexPath:indexPath]).item = weakself.sections.firstObject.items[indexPath.row];
});
} state:^(MJDownloadState state, NSString *file, NSError *error) {
// 主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
if (state == MJDownloadStateCompleted) {
// 更新模型
weakself.sections.firstObject.items[indexPath.row].subTitle = @"播放";
// 更新视图; 获取不到cell就不刷新UI, 下次滚动cell然后赋值模型的时候, 还会刷新模型数据
((LMJSettingCell *)[weakself.tableView cellForRowAtIndexPath:indexPath]).item = weakself.sections.firstObject.items[indexPath.row];
}
});
}];
}else if (info.state == MJDownloadStateCompleted) { // 文件是下载完毕的状态
// 跳转播放, 根据实际情况点击
VIDMoviePlayerViewController *playerVc = [[VIDMoviePlayerViewController alloc] init];
playerVc.videoURL = [NSString stringWithFormat:@"file://%@", info.file];
[weakself.navigationController pushViewController:playerVc animated:YES];
}
}]);
}];
// 添加2个操作模型, 绑定模型itemOperation操作
self.addItem([LMJWordItem itemWithTitle:@"全部开始" subTitle: nil itemOperation:^(NSIndexPath *indexPath) {
[[MJDownloadManager defaultManager] resumeAll];
}]).addItem([LMJWordItem itemWithTitle:@"全部暂停" subTitle: nil itemOperation:^(NSIndexPath *indexPath) {
[[MJDownloadManager defaultManager] suspendAll];
}]);
from iosproject.
尝试用了您这边的数据试了试, 应该是可以的; 建议您参考下cell的数据刷新和赋值模型数据的逻辑
from iosproject.
from iosproject.
from iosproject.
明天demo发下哦, 上传到您自己的github的仓库
from iosproject.
from iosproject.
您好,我这边已经将自己的demo上传至GitHub,地址 :https://github.com/OmgKevin/FileDownload
,请帮忙看一下呀,万分感谢
from iosproject.
Related Issues (20)
- pod install错误 HOT 1
- 工程用不了,不能学习喽 HOT 4
- error: Multiple commands produce
- 项目跑不起来 HOT 1
- 大神,运行报错该怎么解决
- 运行报错 HOT 1
- iOSProject.debug.xcconfig: unable to open file
- 在百思不解项目中,点击我的界面会直接闪退
- No type or protocol named 'UIUserActivityRestoring' HOT 1
- iOS 13 部分动画有问题
- 运行不起来了,初学者
- pod install和pod update都报错[!] Error installing UMengAnalytics友盟的东西
- 项目运行起来 显示内有权限
- 项目跑起来了,但是全都点不了, FPS悬浮框又可以拖动 HOT 1
- MOFSPickerManager 包修改
- User
- Multiple methods named 'orientation' found with mismatched result, parameter type or attributes HOT 1
- library 'iconv.2.4.0' not found HOT 1
- Assertion failed: (aliasSectionNum == sectionNum && "alias and its target must be located in the same section"), function assignAliasAtomOffsetInSection, file Layout.cpp, line 3248.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from iosproject.