一個單選的需求,雖說不到一個下午又被砍掉了,還是記錄下
單選例圖
整個寫下來說白了就是對 Cell 獲取的 IndexPath 的實現和改變,所以首先分析下一般我們是怎樣獲取 cell 上的 indexPath的
1、通過 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;
}
直接明顯,但當數目量多的時候,且包含 section 的時候,就非常不爽了,而且這明顯是 cell 中的事情,拿出來干嘛呢,同時這也不是直接拿到IndexPath 的,不推薦。
2、通過 在 Cell 中通過其父視圖獲取
- (void)raidoSelectAction:(UIButton *)button {
UITableView *tableView = (UITableView *)button.superview.superview.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:self];
}
這樣寫,我可以明白為什么會有四個 superView的,但是有時我們不一定能想明白,而且 iOS7之前在此處是只要三個 superView,對于UITableViewWrapperView 出現當初是坑了不少人的,所以對于這種寫法,也是不推薦的。
3、通過cell 中傳來 tableView 再來獲取 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;
}
我是推薦這種寫法的,只是需要多傳一次而已。
部分完整代碼
#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 {
// 改變狀態
button.selected = !button.selected;
// 當被選中的時候
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 處,只需要瞧瞧下面兩個方法就 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 : 接下來可以保存你選中的做需要做的事情。
}
總的說來,就是對 IndexPath 的獲取,小小記錄下
PS:另外看到了runtime添加屬性方式,獲取 IndexPath,感覺怪怪的,用 runtime 的思路是 OK 的,但是此處它里面的實現確實可以調整的。