以前做离线地图的时候开发的,先占个坑,后续再进行整理出来
原理:设置section header的点击事件,然后刷新对应section的数据源
#import <UIKit/UIKit.h>
typedef enum : NSUInteger {
kOffLineTabviewIsStart, // 开始下载
kOffLineTabviewIsStop, // 暂停下载
} OffLineTabviewIsStartOrStop;
@protocol GroupTableviewDelegate <NSObject>
/**
* 告诉外边下载的指令
*
* @param isStartOrStop 1表示开始下载 0表示暂停下载
* @param cityID 需要下载的城市的ID
*/
- (void)offLineTabviewDelegateDownLoadMap:(OffLineTabviewIsStartOrStop)isStartOrStop withCityID:(int)cityID;
@end
@interface GroupTableview : UITableView<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, weak) id<GroupTableviewDelegate> offLineMapTabviewDelegate;
// 给外面拿到的属性
@property (nonatomic, strong) NSArray *selectArr; // 用来缓存head
// 外面传进来的属性
@property (nonatomic, strong) NSMutableArray *groupHeadArr;
@property (nonatomic, strong) NSMutableArray *downloadMapArr;
- (void)setSearchResults:(NSArray *)searchResults AndIsShowThat:(BOOL)isShow;
@end
================================================================
#import "GroupTableview.h"
#import "groupHeadCell.h"
#import "selectedModel.h"
#import "LROffLineMapController.h"
@interface GroupTableview()<groupHeadCellDelegate>
// 用来做搜索栏的
@property (nonatomic, strong) NSArray *searchResults;
// 用来保存是否选中的信息
@property (nonatomic, strong) NSMutableArray *recordSelectedIndexPath;
@end
@implementation GroupTableview
{
BOOL isShowSearchResultTable;
}
- (NSMutableArray *)recordSelectedIndexPath{
if (_recordSelectedIndexPath == nil) {
_recordSelectedIndexPath = [NSMutableArray array];
}
return _recordSelectedIndexPath;
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
if (self = [super initWithFrame:frame style:style]) {
self.delegate = self;
self.dataSource = self;
isShowSearchResultTable = NO;
}
return self;
}
- (void)setGroupHeadArr:(NSMutableArray *)groupHeadArr{
_groupHeadArr = groupHeadArr;
}
- (void)setDownloadMapArr:(NSMutableArray *)downloadMapArr{
_downloadMapArr = downloadMapArr;
[self reloadData];
}
- (void)setSearchResults:(NSArray *)searchResults AndIsShowThat:(BOOL)isShow{
isShowSearchResultTable = isShow;
self.searchResults = searchResults;
[self reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if (isShowSearchResultTable) {
return 1;
}else{
return self.groupHeadArr.count;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (isShowSearchResultTable) {
return self.searchResults.count;
}else{
selectedModel *model = [self.groupHeadArr objectAtIndex:section];
BOOL isSelected = model.isSelected;
if (isSelected) {
NSArray *citys = model.searchRecord.childCities;
return citys.count;
}else{
return 0;
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"tableCell"];
}
selectedModel *model;
BMKOLSearchRecord *city;
if (isShowSearchResultTable) {
model = [self.searchResults objectAtIndex:indexPath.row];
city = model.searchRecord;
}else{
model = [self.groupHeadArr objectAtIndex:indexPath.section];
NSArray *citys = model.searchRecord.childCities;
city = [citys objectAtIndex:indexPath.row];
}
NSString *cityName = city.cityName;
cell.textLabel.text = cityName;
cell.detailTextLabel.text = [self getDataSizeString:city.size];
// 判断是否已经下载,再看是显示已下载还是显示大小
for (BMKOLUpdateElement* item in self.downloadMapArr) {
if (item.cityID == city.cityID) {
cell.detailTextLabel.text = LIVALL_LocalizedString(@"Livall_305",@"已下载");
}
}
cell.detailTextLabel.textColor = COLOR(53, 53, 53);
if(offMapCtrlStopedCities && offMapCtrlStopedCities.count > 0){
for (BMKOLSearchRecord *stpedItem in offMapCtrlStopedCities) {
if (stpedItem.cityID == city.cityID ) {
cell.detailTextLabel.text = @"暂停";
cell.detailTextLabel.textColor = [UIColor redColor];
}
}
}
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = COLOR(238, 238, 238);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedModel *model;
BMKOLSearchRecord *cityRecord;
if (isShowSearchResultTable) {
// 和底下的逻辑一样
model = [self.searchResults objectAtIndex:indexPath.row];
cityRecord = model.searchRecord;
}else{
model = [self.groupHeadArr objectAtIndex:indexPath.section];
BMKOLSearchRecord *searchRecord = model.searchRecord;
cityRecord = [searchRecord.childCities objectAtIndex:indexPath.row];
}
OffLineTabviewIsStartOrStop statues = kOffLineTabviewIsStart;
// 获取当前的cell是上次已经点击的还是未点击的
NSInteger idx = -1;
for (NSIndexPath *idxPath in self.recordSelectedIndexPath) {
if (idxPath.section == indexPath.section && indexPath.row == idxPath.row) {
statues = kOffLineTabviewIsStop;
idx = [self.recordSelectedIndexPath indexOfObject:idxPath];
// 对应的cell显示暂停下载
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text = @"暂停下载";
}
}
if (idx != -1) {
[self.recordSelectedIndexPath removeObjectAtIndex:idx];
}else{
[self.recordSelectedIndexPath addObject:indexPath];
}
// 告诉外面开始下载或者什么
if (self.offLineMapTabviewDelegate &&
[self.offLineMapTabviewDelegate respondsToSelector:@selector(offLineTabviewDelegateDownLoadMap:withCityID:)]) {
[self.offLineMapTabviewDelegate offLineTabviewDelegateDownLoadMap:statues withCityID:cityRecord.cityID];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (isShowSearchResultTable) {
return 0.001;
}else{
return 44;
}
return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 1;
}
- (NSArray *)selectArr{
if (_selectArr == nil) {
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i<[self numberOfSections]; i++) {
groupHeadCell *headCell = [[groupHeadCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"headGroup"];
headCell.delegte = self;
headCell.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
UIImageView *underImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lmp_offline_map_under_arrow_img"]];
headCell.accessoryView = underImgV;
[arr addObject:headCell];
}
_selectArr = [NSArray arrayWithArray:arr];
}
return _selectArr;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (isShowSearchResultTable) {
return nil;
}else{
groupHeadCell *headCell = [self.selectArr objectAtIndex:section];
selectedModel *model = [self.groupHeadArr objectAtIndex:section];
BMKOLSearchRecord *searchRecord = model.searchRecord;
if (searchRecord.cityType == 0 || searchRecord.cityType == 2) {
// 判断是否已经下载,再看是显示已下载还是显示大小
headCell.accessoryView = nil;
headCell.detailTextLabel.text = [self getDataSizeString:searchRecord.size];
for (BMKOLUpdateElement* item in self.downloadMapArr) {
if (item.cityID == searchRecord.cityID) {
headCell.detailTextLabel.text = LIVALL_LocalizedString(@"Livall_305",@"已下载");
}
}
headCell.detailTextLabel.textColor = COLOR(53, 53, 53);
if(offMapCtrlStopedCities && offMapCtrlStopedCities.count > 0){
for (BMKOLSearchRecord *stpedItem in offMapCtrlStopedCities) {
if (stpedItem.cityID == searchRecord.cityID ) {
headCell.detailTextLabel.text = @"暂停";
headCell.detailTextLabel.textColor = [UIColor redColor];
}
}
}
}
headCell.textLabel.text = model.searchRecord.cityName;
headCell.tag = section;
return headCell;
}
}
- (void)groupHeadCellDelegateDidClicked:(groupHeadCell *)groupHeadCell{
selectedModel *model = [self.groupHeadArr objectAtIndex:groupHeadCell.tag];
BMKOLSearchRecord *searchRecord = model.searchRecord;
if (groupHeadCell.isSelected) {
if (searchRecord.cityType == 0 || searchRecord.cityType == 2) {
// 如果是全国包或直辖市,则直接下载
if (self.offLineMapTabviewDelegate &&
[self.offLineMapTabviewDelegate respondsToSelector:@selector(offLineTabviewDelegateDownLoadMap:withCityID:)]) {
[self.offLineMapTabviewDelegate offLineTabviewDelegateDownLoadMap:kOffLineTabviewIsStart withCityID:searchRecord.cityID];
}
}else{
model.isSelected = YES;
}
}else{
if (searchRecord.cityType == 0 || searchRecord.cityType == 2) {
// 如果是全国包或直辖市,则暂停下载
groupHeadCell.detailTextLabel.text = @"暂停下载";
// 暂停下载的时候显示暂停下载
if (self.offLineMapTabviewDelegate &&
[self.offLineMapTabviewDelegate respondsToSelector:@selector(offLineTabviewDelegateDownLoadMap:withCityID:)]) {
[self.offLineMapTabviewDelegate offLineTabviewDelegateDownLoadMap:kOffLineTabviewIsStop withCityID:searchRecord.cityID];
}
}else{
model.isSelected = NO;
}
}
[self reloadData];
}
- (void)getAllSize{
int allSize = 0;
for (selectedModel *model in self.groupHeadArr) {
BMKOLSearchRecord *result = model.searchRecord;
if (result.cityType == 0 || result.cityType == 2) {
allSize += result.size;
}else{
for (BMKOLSearchRecord *child in result.childCities) {
allSize += child.size;
}
}
ZW_DEBUG_Log(@"所有的大小为%@",[self getDataSizeString:allSize]);
allSize = 0;
}
ZW_DEBUG_Log(@"所有的大小为%@",[self getDataSizeString:allSize]);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self endEditing:YES];
}
#pragma mark - 包大小转换工具类(将包大小转换成合适单位)
-(NSString *)getDataSizeString:(int) nSize
{
NSString *string = nil;
if (nSize<1024)
{
string = [NSString stringWithFormat:@"%dB", nSize];
}
else if (nSize<1048576)
{
string = [NSString stringWithFormat:@"%dK", (nSize/1024)];
}
else if (nSize<1073741824)
{
if ((nSize%1048576)== 0 )
{
string = [NSString stringWithFormat:@"%dM", nSize/1048576];
}
else
{
int decimal = 0; //小数
NSString* decimalStr = nil;
decimal = (nSize%1048576);
decimal /= 1024;
if (decimal < 10)
{
decimalStr = [NSString stringWithFormat:@"%d", 0];
}
else if (decimal >= 10 && decimal < 100)
{
int i = decimal / 10;
if (i >= 5)
{
decimalStr = [NSString stringWithFormat:@"%d", 1];
}
else
{
decimalStr = [NSString stringWithFormat:@"%d", 0];
}
}
else if (decimal >= 100 && decimal < 1024)
{
int i = decimal / 100;
if (i >= 5)
{
decimal = i + 1;
if (decimal >= 10)
{
decimal = 9;
}
decimalStr = [NSString stringWithFormat:@"%d", decimal];
}
else
{
decimalStr = [NSString stringWithFormat:@"%d", i];
}
}
if (decimalStr == nil || [decimalStr isEqualToString:@""])
{
string = [NSString stringWithFormat:@"%dMss", nSize/1048576];
}
else
{
string = [NSString stringWithFormat:@"%d.%@M", nSize/1048576, decimalStr];
}
}
}
else // >1G
{
string = [NSString stringWithFormat:@"%dG", nSize/1073741824];
}
return string;
}
@end
===================================================