gpt4 book ai didi

ios - 如何在我编辑该单元格时保持 UITableViewCell 处于选中状态?

转载 作者:行者123 更新时间:2023-11-29 10:56:08 25 4
gpt4 key购买 nike

我正在制作一个应用程序来跟踪类(class)中不同的学生对象。每个学生都有学号、姓名和余额。我已经为它的工作方式创建了所有后端逻辑,并且我将所有学生的显示在 UITableView 中。然后我有按钮,按下时会增加/减少所选学生的货币值(value)。唯一的问题是,每次我在表格 View 中选择一个学生,然后按下增加/减少金钱按钮时,它都会取消选择该单元格。我希望该单元格保持选中状态,这样我就可以一直按增加/减少金额按钮。每次我按下增加/减少钱按钮时,我还需要更新单元格,以便它会显示该学生在单元格中上升或下降的钱。

这是增加/减少钱按钮的代码之一:

- (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;
}

我的更新界面方法:

- (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];
}

一切正常,但我只希望在按下钱按钮时当前单元格保持选中状态。

任何帮助都会很棒!非常感谢!

最佳答案

reloadRowsAtIndexPaths:withRowAnimation: 在调用时取消选择单元格。

因为您在 - (void)updateUI 中已经有了 indexPath 并且没有修改 tableview,请尝试通过调用

以编程方式再次选择该行
[self.testTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

reloadRowsAtIndexPaths:withRowAnimation: 之后。

关于ios - 如何在我编辑该单元格时保持 UITableViewCell 处于选中状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18342861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com