1

我正在制作一个应用程序来跟踪不同学生对象作为班级的一部分。每个学生都有一个学号、姓名和余额。我已经为它的工作方式创建了所有后端逻辑,并且我将所有学生都显示在 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];
}

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

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

4

2 回答 2

1

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

由于您已经在indexPath里面- (void)updateUI并且没有修改 tableview,请尝试通过调用再次以编程方式选择行

[self.testTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

之后reloadRowsAtIndexPaths:withRowAnimation:

于 2013-08-20T19:20:32.230 回答
1

在 XIB 中,对于 TableView,您可以将属性“Editing”设置为“No Selection during Editing”或“Single Selection during Editing”。

此外,您可以手动调用:

[self.testTableView selectRowAtIndexPath:IndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];

在您的更新用户界面中。希望这会有所帮助。

于 2013-08-20T19:21:58.143 回答