Ios 使用节将indexPath减少一

Ios 使用节将indexPath减少一,ios,objective-c,iphone,cocoa-touch,uitableview,Ios,Objective C,Iphone,Cocoa Touch,Uitableview,我有一个带有节的UITableView,我想知道给定indepath的前一项,我知道我可以使用它 nsindepath*newindepath=[nsindepath indexPathForRow:oldIndexPath.row-1片段:oldIndexPath.section] 如果我只有一个部分,但由于我有几个部分,我如何知道tableview中的上一个项目(如果该项目在他的部分中是第一个,我希望获得上一个部分的最后一个项目) 谢谢。如果(第1行)小于0,则从节中减去一。 如果(节-1)

我有一个带有节的
UITableView
,我想知道给定
indepath
的前一项,我知道我可以使用它

nsindepath*newindepath=[nsindepath indexPathForRow:oldIndexPath.row-1片段:oldIndexPath.section]

如果我只有一个部分,但由于我有几个部分,我如何知道tableview中的上一个项目(如果该项目在他的部分中是第一个,我希望获得上一个部分的最后一个项目)

谢谢。

如果(第1行)小于0,则从节中减去一。 如果(节-1)也小于0,则表示您位于表的顶部。否则,请查找该节的最后一行。 您必须拥有该信息,因为您将其提供给了表视图。

如果(行-1)小于0,请从节中减去一。 如果(节-1)也小于0,则表示您位于表的顶部。否则,请查找该节的最后一行。
您必须拥有这些信息,因为您将其提供给了表视图。

我认为您可以这样做:(如果您在接收
UITableView
对象作为参数的方法中使用此代码,则将
self.tableView
替换为
tableView
):


我认为您可以这样做:(如果您在接收
UITableView
对象作为参数的方法中使用此代码,则将
self.tableView
替换为
tableView
):


谢谢!我最后用了别的东西,但这个效果很好!谢谢!我最后用了别的东西,但这个效果很好!
NSIndexPath* newIndexPath = nil;
// check for row being the first one in this section
if (oldIndexPath.row == 0) {
    // check for this section having a previous section
    if (oldIndexPath.section > 0) {
        // get number of rows of the previous section
        NSInteger numberOfRows = [self.tableView numberOfRowsInSection : oldIndexPath.section - 1];
        newIndexPath = [NSIndexPath indexPathForRow: numberOfRows - 1 inSection: oldIndexPath.section - 1];
    }
} else { // if the row is not the first in this section
    // get the indexPath for the previous row in this section
    newIndexPath = [NSIndexPath indexPathForRow:oldIndexPath.row - 1 inSection: oldIndexPath.section];
}