转自: https://blog.csdn.net/qq_36747738/article/details/60867635
转自: https://blog.csdn.net/qq_29284809/article/details/50057989
//选择转账用户对勾
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger newRow = [indexPathrow];
NSInteger oldRow = (_lastPath ==nil)?[_lastPathrow]:-1;
if (newRow != oldRow) {
UITableViewCell *newCell = [tableViewcellForRowAtIndexPath:indexPath];
newCell.accessoryType =UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableViewcellForRowAtIndexPath:_lastPath];
oldCell.accessoryType =UITableViewCellAccessoryNone;
_lastPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
_selectSourceM = _dataSourceM[newRow];
}
下面是自定义的单选
创建Model 设置个BOOL值
#import <Foundation/Foundation.h>
@interface Model : NSObject
@property(nonatomic, assign) BOOL isSelected;
@end
cell.h
#import <UIKit/UIKit.h>
@class Model;
@interface LYTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property(assign, nonatomic)NSInteger clickCount;
- (void)cellWithData:(Model *)model;
自定义cell .m
- (void)cellWithData:(Model *)model {
if (model.isSelected) {
[self.btn setBackgroundImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
} else {
[self.btn setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
[cell.btn addTarget:self action:@selector(ClickBtn:) forControlEvents:UIControlEventTouchUpInside];
cell.btn.tag = indexPath.row;
Model *model =self.arrays[indexPath.row];
[cell cellWithData:model];
return cell;
}
-(void)ClickBtn:(UIButton *)btn
{
if (self.selectModel) {
self.selectModel.isSelected = !self.selectModel.isSelected;
}
Model *model = self.arrays[btn.tag];
if (!model.isSelected) {
model.isSelected = !model.isSelected;
self.selectModel = model;
}
[self.tableView reloadData];
}
最近在做一个页面关于UITableViewCell的单选,就是选中某行时后面有对勾或者是标记选中的那行,整理了一下,方便各大网友。下面就说下实现的方法。
第一种的实现
在.m的文件里:
#import "MyViewController.h"
#import "NextViewController.h"
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, assign)BOOL ifSelected;//是否选中
@property (nonatomic, strong)NSIndexPath * lastSelected;//上一次选中的索引
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
在viewDidLoad里面的实现:
self.ifSelected = NO;//是否被选中 默认为NO
- 1
- 2
在TableView数据源方法里面:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIndentifier = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
}
if (indexPath.row == 0) {
cell.textLabel.text = @"男";
}else{
cell.textLabel.text = @"女";
}
if (self.ifSelected) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
在tableview代理方法里面:
#pragma mark -tableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath * temp = self.lastSelected;//暂存上一次选中的行
if (temp && temp != indexPath)//如果上一次的选中的行存在,并且不是当前选中的这一行,则让上一行不选中
{
self.ifSelected = NO;//修改之前选中的cell的数据为不选中
[tableView reloadRowsAtIndexPaths:@[temp] withRowAnimation:UITableViewRowAnimationAutomatic];//刷新该行
}
self.lastSelected = indexPath;//选中的修改为当前行
self.ifSelected = YES;//修改这个被选中的一行
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//重新刷新
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
实现的效果图:
第二种实现方法
.m文件里面:
#import "AdViewController.h"
#import "NextViewController.h"
@interface AdViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, retain) NSIndexPath *selectedIndexPath;
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
在TableView数据源里面的方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIndentifier = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
}
if ([self.selectedIndexPath isEqual:indexPath]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
在tableView代理方法里面:
#pragma mark -tableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.selectedIndexPath) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedIndexPath = indexPath;
}