一個(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)整的存淫。