PlaceAroundTableView.m 7.3 KB
//
//  PlaceAroundTableView.m
//  CMDataCollectTool
//
//  Created by PC on 15/8/7.
//  Copyright (c) 2015年 autonavi. All rights reserved.
//

#import "PlaceAroundTableView.h"

#define kMoreButtonTitle @"更多..."

@interface PlaceAroundTableView()

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) AMapPOI *selectedPoi;

@property (nonatomic, assign) BOOL isFromMoreButton;
@property (nonatomic, strong) UIButton *moreButton;

@end

@implementation PlaceAroundTableView

#pragma mark - Interface

- (AMapPOI *)selectedTableViewCellPoi
{
    return self.selectedPoi;
}

#pragma mark - AMapSearchDelegate

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if (self.isFromMoreButton == YES)
    {
        self.isFromMoreButton = NO;
    }
    else
    {
        [self.searchPoiArray removeAllObjects];
        [self.moreButton setTitle:kMoreButtonTitle forState:UIControlStateNormal];
        self.moreButton.enabled = YES;
        self.moreButton.backgroundColor = [UIColor whiteColor];
    }

    if (response.pois.count == 0)
    {
        [self.moreButton setTitle:@"没有数据了.." forState:UIControlStateNormal];
        self.moreButton.enabled = NO;
        self.moreButton.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
        
        self.selectedIndexPath = nil;
        [self.tableView reloadData];
        return;
    }
    
    [response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
        [self.searchPoiArray addObject:obj];
    }];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"noti_sendButton" object:nil];
    self.selectedIndexPath = nil;
    [self.tableView reloadData];
}

- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil)
    {
        self.currentAddress = response.regeocode.formattedAddress;
        self.cityAddress = response.regeocode.addressComponent;
        NSIndexPath *reloadIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView reloadRowsAtIndexPaths:@[reloadIndexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}

#pragma mark - UITableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType= UITableViewCellAccessoryCheckmark;
    self.selectedIndexPath = indexPath;
    [tableView reloadData];
    if (indexPath.section == 0)
    {
        self.selectedPoi = nil;
        [self.delegate didPositionCellTapped];
        return ;
    }
    
    self.selectedPoi = self.searchPoiArray[indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(didTableViewSelectedChanged:)])
    {
        [self.delegate didTableViewSelectedChanged:self.selectedPoi];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

#pragma mark - UITableView Datasource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reusedIndentifier = @"reusedIndentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedIndentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reusedIndentifier];
    }
    if (indexPath.section == 0)
    {
        cell.textLabel.text = @"[位置]";
        // 如果没有地址,就设置成"未知"
      if([CommonFun isBlankOrNullString:self.currentAddress]){
            self.currentAddress = @"未知";
        }
        cell.detailTextLabel.text = self.currentAddress;
    }
    else
    {
        AMapPOI *poi = self.searchPoiArray[indexPath.row];
        cell.textLabel.text = poi.name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@%@%@",poi.province,poi.city,poi.district,poi.address];
        cell.detailTextLabel.textColor = UIColorFromRGB(0x999999, 1.0);
    }
    
    if (self.selectedIndexPath && self.selectedIndexPath.section == indexPath.section && self.selectedIndexPath.row == indexPath.row)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else if (!self.selectedIndexPath && indexPath.row == 0)
    {
        self.selectedIndexPath = indexPath;
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
  
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else
    {
        return self.searchPoiArray.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark - Handle Action

- (void)actionMoreButtonTapped
{
    // 防止快速连续点两次
    if (self.isFromMoreButton == YES)
    {
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(didLoadMorePOIButtonTapped)])
    {
        self.isFromMoreButton = YES;
        [self.delegate didLoadMorePOIButtonTapped];
    }
}

#pragma mark - Initialization

- (NSMutableArray *)searchPoiArray
{
    if (_searchPoiArray == nil)
    {
        _searchPoiArray = [[NSMutableArray alloc] init];
    }
    return _searchPoiArray;
}

- (void)initTableViewFooter
{
#define kMoreButtonMargin   20
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 60)];
    
    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    moreBtn.frame = footer.bounds;
    [moreBtn setTitle:kMoreButtonTitle forState:UIControlStateNormal];
    [moreBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [moreBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.4] forState:UIControlStateHighlighted];
    moreBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    moreBtn.titleEdgeInsets = UIEdgeInsetsMake(0, kMoreButtonMargin, 0, 0);
    [moreBtn addTarget:self action:@selector(actionMoreButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    self.moreButton = moreBtn;
    
    [footer addSubview:moreBtn];
    
    UIView *upLineView = [[UIView alloc] initWithFrame:CGRectMake(kMoreButtonMargin, 3, ScreenWidth - kMoreButtonMargin, 0.5)];
    upLineView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
    [footer addSubview:upLineView];
    
    self.tableView.tableFooterView = footer;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        self.isFromMoreButton = NO;
        
        [self addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [self initTableViewFooter];
    }
    
    return self;
}


@end