#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,assign) NSInteger currentIndex;
@end
static NSString *identifier = @"chooseCell";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[ self loadSubview];
// Do any additional setup after loading the view, typically from a nib.
}
// 加载视图的方法
- (void)loadSubview
{
// *********************** 初始化tableView ***************************
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0 , 320, 560) style:UITableViewStylePlain];
self.tableView.backgroundColor = [UIColor greenColor];
// 设置代理
self.tableView.delegate = self;
self.tableView.dataSource = self;
// 注册cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
// 添加到根视图上
[self.view addSubview:self.tableView];
}
#pragma mark - tableViewe的代理方法
// 返回分区中cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 26;
}
// 返回分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// 返回cell的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.selectionStyle = UITableViewCellAccessoryCheckmark; //选中cell时无色
}
cell.textLabel.text = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"].mutableCopy[indexPath.row];
return cell;
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==_currentIndex){
return UITableViewCellAccessoryCheckmark;
}
else{
return UITableViewCellAccessoryNone;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if(indexPath.row==_currentIndex){
return;
}
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:_currentIndex
inSection:0];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
_currentIndex=indexPath.row;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
单选 -- 点击显示“对号”功能
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 直接上代码 首先定义一个显示大图的协议 然后添加协议extension---1.第一个是不带动画的直接模态显示出大...