我正在制作一个应用程序来跟踪不同学生对象作为班级的一部分。每个学生都有一个学号、姓名和余额。我已经为它的工作方式创建了所有后端逻辑,并且我将所有学生都显示在 UITableView 中。然后我有按钮,按下时会增加/减少所选学生的货币价值。唯一的问题是,每次我在表格视图中选择一个学生然后按下增加/减少钱按钮时,它都会取消选择单元格。我希望该单元格保持选中状态,这样我就可以一直按下递增/递减货币按钮。每次按下增加/减少金钱按钮时,我还需要更新单元格,以便它将显示该学生在单元格中上升或下降的钱。
这是增加/减少钱按钮的代码之一:
- (IBAction)twentyDollarButton:(UIButton *)sender
{
self.currentStudent.studentMoney += self.signValue * 20;
[self updateUI];
}
- (EconoClassStudent *)currentStudent
{
if (!_currentStudent) {
_currentStudent = [[EconoClassStudent alloc] init];
} else {
NSIndexPath *indexPath = [self.testTableView indexPathForSelectedRow];
NSArray *instructorClass = [self.instructorClass classAsArray];
if (indexPath && [instructorClass count] > 0) {
EconoClassStudent *student = instructorClass[indexPath.row];
_currentStudent = student;
}
}
return _currentStudent;
}
我的更新 UI 方法:
- (void)updateUI
{
NSIndexPath *indexPath = [self.testTableView indexPathForSelectedRow];
NSArray *indexPathsArray = [[NSArray alloc] initWithObjects:indexPath, nil];
self.studentLabel.text = [NSString stringWithFormat:@"%u. %@\n$%d",
self.currentStudent.studentNumber,
self.currentStudent.studentName,
self.currentStudent.studentMoney];
[self.testTableView reloadRowsAtIndexPaths:indexPathsArray withRowAnimation:NO];
}
一切正常,但我只想在按下钱按钮时保持选中当前单元格。
任何帮助都会很棒!非常感谢!