- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_lastIndex = [NSIndexPath indexPathForRow:0 inSection:0];
[_tableV selectRowAtIndexPath:_lastIndex animated:YES scrollPosition:UITableViewScrollPositionTop];
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
[self tableView:self.tableV didSelectRowAtIndexPath:path];
}
- (void)viewDidLoad {
[super viewDidLoad];
// _lastIndex.row = 0;
// Do any additional setup after loading the view, typically from a nib.
_tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height ) style:UITableViewStylePlain];
_tableV.backgroundColor = [UIColor whiteColor];
_tableV.delegate = self;
_tableV.dataSource = self;
[self.view addSubview:_tableV];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *iden = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden];
}
NSInteger row = [indexPath row];
NSInteger oldRow = [_lastIndex row];
if (row == oldRow && _lastIndex!=nil) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
tableView.separatorStyle = UITableViewCellAccessoryNone;
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger newRow = [indexPath row];
NSInteger oldRow = (_lastIndex !=nil)?[_lastIndex row]:-1;
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndex];
oldCell.accessoryType = UITableViewCellAccessoryNone;
_lastIndex = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}