第二种实现方法
.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;
}
转载地址http://blog.csdn.net/qq_29284809/article/details/50057989