UITableView 單選

一個(gè)單選的需求,雖說(shuō)不到一個(gè)下午又被砍掉了斟叼,還是記錄下

單選例圖

整個(gè)寫(xiě)下來(lái)說(shuō)白了就是對(duì) Cell 獲取的 IndexPath 的實(shí)現(xiàn)和改變纸肉,所以首先分析下一般我們是怎樣獲取 cell 上的 indexPath的

1端逼、通過(guò) Tag 值直接獲取

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RadioCell *cell =[tableView dequeueReusableCellWithIdentifier:@"RadioCellIden" forIndexPath:indexPath];
    cell.tag = indexPath.row;
    [cell.radioButton addTarget:self action:@selector(radioSelectedWithRow:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

直接明顯颜矿,但當(dāng)數(shù)目量多的時(shí)候,且包含 section 的時(shí)候找岖,就非常不爽了陨倡,而且這明顯是 cell 中的事情,拿出來(lái)干嘛呢许布,同時(shí)這也不是直接拿到IndexPath 的兴革,不推薦。

2蜜唾、通過(guò) 在 Cell 中通過(guò)其父視圖獲取

- (void)raidoSelectAction:(UIButton *)button {
    UITableView *tableView = (UITableView *)button.superview.superview.superview.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:self];
}

這樣寫(xiě)杂曲,我可以明白為什么會(huì)有四個(gè) superView的,但是有時(shí)我們不一定能想明白袁余,而且 iOS7之前在此處是只要三個(gè) superView,對(duì)于UITableViewWrapperView 出現(xiàn)當(dāng)初是坑了不少人的擎勘,所以對(duì)于這種寫(xiě)法,也是不推薦的颖榜。

3棚饵、通過(guò)cell 中傳來(lái) tableView 再來(lái)獲取 IndexPath

@interface RadioCell : UITableViewCell
@property (nonatomic, weak) UITableView *tableView;
@end
- (void)raidoSelectAction:(UIButton *)button {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:self];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RadioCell *cell =[tableView dequeueReusableCellWithIdentifier:@"RadioCellIden" forIndexPath:indexPath];
    cell.tableView = tableView;
    return cell;
}

我是推薦這種寫(xiě)法的,只是需要多傳一次而已掩完。

部分完整代碼
#import <UIKit/UIKit.h>

@class RadioModel;
@protocol RadioSelectDelegate;

@interface RadioCell : UITableViewCell

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) id <RadioSelectDelegate> delegate;
@property (nonatomic, strong) RadioModel *model;

@end

@protocol RadioSelectDelegate <NSObject>

- (void)radioSelectedWithIndexPath:(NSIndexPath *)indexPath;

@end
#import "RadioCell.h"
#import <Masonry/Masonry.h>
#import "RadioModel.h"

@interface RadioCell ()

@property (nonatomic, strong) UIButton *radioButton;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation RadioCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_radioButton setImage:[UIImage imageNamed:@"like_default"] forState:UIControlStateNormal];
        [_radioButton setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateSelected];
        [_radioButton addTarget:self action:@selector(raidoSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_radioButton];
        [_radioButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.leading.mas_equalTo(@20);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        
        _nameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(_radioButton.mas_trailing).offset(20);
            make.trailing.mas_equalTo(@(-20));
            make.centerY.equalTo(_radioButton.mas_centerY);
            make.height.mas_equalTo(@30);
        }];
    }
    return self;
}

- (void)raidoSelectAction:(UIButton *)button {
    // 改變狀態(tài)
    button.selected = !button.selected;
    // 當(dāng)被選中的時(shí)候
    if (button.selected) {
        // 獲取 indexPath
        NSIndexPath *indexPath = [self.tableView indexPathForCell:self];
        if (self.delegate && [self.delegate respondsToSelector:@selector(radioSelectedWithIndexPath:)]) {
            [self.delegate radioSelectedWithIndexPath:indexPath];
        }
    }
}

- (void)setModel:(RadioModel *)model {
    self.radioButton.selected = model.isSelected;
    self.nameLabel.text = model.titleString;
}

@end
#import <Foundation/Foundation.h>

@interface RadioModel : NSObject

@property (nonatomic, copy) NSString *titleString;
@property (nonatomic, assign) BOOL isSelected;

@end

此 TableView 處噪漾,只需要瞧瞧下面兩個(gè)方法就 OK 了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RadioCell *cell =[tableView dequeueReusableCellWithIdentifier:RadioTableViewIden forIndexPath:indexPath];
    cell.tableView = tableView;
    cell.delegate = self;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (void)radioSelectedWithIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *tempIndexPath = self.lastIndexPath;
    // 改變上一次的
    if (tempIndexPath && tempIndexPath != indexPath) {

        RadioModel *model =  self.dataArray[tempIndexPath.row];
        model.isSelected = NO;
        [self.tableView reloadRowsAtIndexPaths:@[tempIndexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
    // 記住這一次的
    RadioModel *model =  self.dataArray[indexPath.row];
    model.isSelected = YES;
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    self.lastIndexPath = indexPath;
   // TODO : 接下來(lái)可以保存你選中的做需要做的事情。
}

總的說(shuō)來(lái)且蓬,就是對(duì) IndexPath 的獲取欣硼,小小記錄下
PS:另外看到了runtime添加屬性方式,獲取 IndexPath恶阴,感覺(jué)怪怪的诈胜,用 runtime 的思路是 OK 的,但是此處它里面的實(shí)現(xiàn)確實(shí)可以調(diào)整的存淫。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末耘斩,一起剝皮案震驚了整個(gè)濱河市沼填,隨后出現(xiàn)的幾起案子桅咆,更是在濱河造成了極大的恐慌,老刑警劉巖坞笙,帶你破解...
    沈念sama閱讀 218,036評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件岩饼,死亡現(xiàn)場(chǎng)離奇詭異荚虚,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)籍茧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)版述,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人寞冯,你說(shuō)我怎么就攤上這事渴析。” “怎么了吮龄?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,411評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵俭茧,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我漓帚,道長(zhǎng)母债,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,622評(píng)論 1 293
  • 正文 為了忘掉前任尝抖,我火速辦了婚禮毡们,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘昧辽。我一直安慰自己衙熔,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布搅荞。 她就那樣靜靜地躺著青责,像睡著了一般。 火紅的嫁衣襯著肌膚如雪取具。 梳的紋絲不亂的頭發(fā)上脖隶,一...
    開(kāi)封第一講書(shū)人閱讀 51,521評(píng)論 1 304
  • 那天,我揣著相機(jī)與錄音暇检,去河邊找鬼产阱。 笑死,一個(gè)胖子當(dāng)著我的面吹牛块仆,可吹牛的內(nèi)容都是我干的构蹬。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼悔据,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼庄敛!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起科汗,我...
    開(kāi)封第一講書(shū)人閱讀 39,200評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤藻烤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體怖亭,經(jīng)...
    沈念sama閱讀 45,644評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡涎显,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了兴猩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片期吓。...
    茶點(diǎn)故事閱讀 39,953評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖倾芝,靈堂內(nèi)的尸體忽然破棺而出讨勤,到底是詐尸還是另有隱情,我是刑警寧澤晨另,帶...
    沈念sama閱讀 35,673評(píng)論 5 346
  • 正文 年R本政府宣布悬襟,位于F島的核電站,受9級(jí)特大地震影響拯刁,放射性物質(zhì)發(fā)生泄漏脊岳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評(píng)論 3 329
  • 文/蒙蒙 一垛玻、第九天 我趴在偏房一處隱蔽的房頂上張望割捅。 院中可真熱鬧,春花似錦帚桩、人聲如沸亿驾。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,889評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)莫瞬。三九已至,卻和暖如春郭蕉,著一層夾襖步出監(jiān)牢的瞬間疼邀,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,011評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工召锈, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留旁振,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,119評(píng)論 3 370
  • 正文 我出身青樓涨岁,卻偏偏與公主長(zhǎng)得像拐袜,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子梢薪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容