20141013個人日誌(button的tag巧用,在tableView的代理方法外面取響應cell的方法)

    項目裏有這麼個問題,在 tableView 的 cell 上添加按鈕,因爲涉及到 cell 的重用,cell 上的按鈕也會被重用。比如,用 tableView 顯示一些需要下載的條目,每個條目右側可安放一個“下載”按鈕,這個按鈕就是會被重用的。按鈕有響應事件,那就得在響應事件裏區分是哪個 button 的響應事件。

    通常在 tableView 的代理方法裏給button設置不同的tag(button自己本身有tag屬性),然後就可以根據這個tag來區分響應的button事件。響應事件裏默認的一個參數就是(id)sender,實際上也就是(UIButton*)sender

這個sender就是button對象自己。用 [sender tag] 作爲switch的參數可區分相應的事件。

    我之前是這麼想的,因爲 button 是加在 cell.contentView 上的,用方法[cell.contentView viewWithTag:tag]就可以取出相應的button。具體操作如下:

<span style="white-space:pre">	</span><span style="font-size:18px;">NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; //取出indexPath
      UIButton *btn = (UIButton *)[[self.tableView cellForRowAtIndexPath:indexPath].contentView viewWithTag:0];             </span>
<span style="font-size:18px;"><span style="white-space:pre">	</span>   //用相應的indexPath來找相應的cell
        [btn setEnabled:NO];   //可以設置相應的button屬性了</span>

    今天完成清除地圖緩存裏的bug修改。bug表現爲:上述的cell上的按鈕,點擊清除緩存後仍然可以繼續點擊,因爲button是共用的,不太好處理點擊事件,需要分別設置不同的狀態變量來區分。

狀態:

typedef enum whichAlertViewToShow
{
    SearchHistoryCacheClearUpType,
    clearUpSearchHistorySuccessed,
    clearUpSearchHistoryFailed,
    
    MapCacheClearUpType,
    ClearUpMapCacheSuccessed,
    clearUpMapCacheFailed,
}AlertViewToShowType;

定義變量

BOOL _isSearchHistoryCacheClearUp;
BOOL _isMapCacheClearUp;
AlertViewToShowType _showType;

初始化

        _isSearchHistoryCacheClearUp = NO;
        _isMapCacheClearUp           = NO;

button的響應事件函數

-(void)pressRouteCalculate:(UIButton*)sender
{
    switch ([sender tag])
    {
        case 0:
            if (!_isSearchHistoryCacheClearUp)
            {
                _showType = SearchHistoryCacheClearUpType;
                CMAlertView * alert = [[CMAlertView alloc] initWithTitle:@"提醒"
                                                                 message:@"是否清除搜索歷史記錄"
                                                                delegate:self];
                [alert show];
                [alert release];
        }
            break;
        case 1:
            if (!_isMapCacheClearUp) {
                _showType = MapCacheClearUpType;
                CMAlertView * alert = [[CMAlertView alloc] initWithTitle:@"提醒"
                                                                 message:@"是否清除地圖緩存"
                                                                delegate:self];
                [alert show];
                [alert release];
            }
            break;
        default:
            break;
    }
}

警告框的done按鈕響應的代理方法

-(void)clickDoneButton:(CMAlertView *)alertView
{
    if (_showType == SearchHistoryCacheClearUpType)
    {
        if([[CMHistoryRecordCenter shared] removeWithType:CMHistoryRecordTypeKeySearch])
        {
            _isSearchHistoryCacheClearUp = YES;
            _showType = clearUpSearchHistorySuccessed;
            CMAlertView * alert = [[CMAlertView alloc] initWithTitle:@"提醒"
                                                             message:@"已經清除搜索歷史記錄!"
                                                            delegate:nil];
            [alert show];
            [alert release];
            [self.tableView reloadData];
        }
        else
        {
            _isSearchHistoryCacheClearUp = NO;
            _showType = clearUpSearchHistoryFailed;
            CMAlertView * alert = [[CMAlertView alloc] initWithTitle:@"提醒"
                                                             message:@"清空失敗!"
                                                            delegate:nil];
            [alert show];
            [alert release];
        }
    }
    else if (_showType == MapCacheClearUpType)
    {
        if([[SystemConfig shared] deleteMapCache])
        {
            _isMapCacheClearUp = YES;
            _showType = ClearUpMapCacheSuccessed;
            CMAlertView * alert = [[CMAlertView alloc] initWithTitle:@"提醒"
                                                             message:@"地圖緩存已經清空!"
                                                            delegate:nil];
            [alert show];
            [alert release];
            [self.tableView reloadData];
        }
        else
        {
            _isMapCacheClearUp = NO;
            _showType = clearUpMapCacheFailed;
            CMAlertView * alert = [[CMAlertView alloc] initWithTitle:@"提醒"
                                                             message:@"清空失敗!"
                                                            delegate:nil];
            [alert show];
            [alert release];
        }
    }
}



還修改了下載離線地圖以及路口放大圖的進度條顯示問題等。




發佈了33 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章