Как увеличить NSIndexPath - PullRequest
       13

Как увеличить NSIndexPath

6 голосов
/ 09 марта 2012

У меня есть ситуация с данными, когда я хочу использовать индексный путь.Когда я пересекаю данные, я хочу увеличить последний узел NSIndexPath.Код, который у меня есть на данный момент:

int nbrIndex = [indexPath length];
NSUInteger *indexArray = (NSUInteger *)calloc(sizeof(NSUInteger),nbrIndex);
[indexPath getIndexes:indexArray];
indexArray[nbrIndex - 1]++;
[indexPath release];
indexPath = [[NSIndexPath alloc] initWithIndexes:indexArray length:nbrIndex];
free(indexArray);

Это немного, ну неуклюже - Есть ли лучший способ сделать это?

Ответы [ 4 ]

6 голосов
/ 09 марта 2012

Вы можете попробовать это - возможно, одинаково неуклюже, но, по крайней мере, немного короче:

NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];
5 голосов
/ 09 августа 2012

Так на одну строку меньше:

indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:actualIndexPath.section];

3 голосов
/ 30 октября 2015

Проверьте мое решение на Swift:

func incrementIndexPath(indexPath: NSIndexPath) -> NSIndexPath? {
    var nextIndexPath: NSIndexPath?
    let rowCount = numberOfRowsInSection(indexPath.section)
    let nextRow = indexPath.row + 1
    let currentSection = indexPath.section

    if nextRow < rowCount {
        nextIndexPath = NSIndexPath(forRow: nextRow, inSection: currentSection)
    }
    else {
        let nextSection = currentSection + 1
        if nextSection < numberOfSections {
            nextIndexPath = NSIndexPath(forRow: 0, inSection: nextSection)
        }
    }

    return nextIndexPath
}
1 голос
/ 31 августа 2018

Цикл for в Swift 4, обеспечивающий аналогичные результаты с помощью встроенного UITableView, повторяющий цикл for, заполняющий текст детализации ячейки «Row Обновлено»

for i in 0 ..< 9 {
     let nextRow = (indexPath?.row)! + i
     let currentSection = indexPath?.section
     let nextIndexPath = NSIndexPath(row: nextRow, section: currentSection!)

     embeddedViewController.tableView.cellForRow(at: nextIndexPath as IndexPath)?.detailTextLabel?.text = "Row Updated"

     let myTV = embeddedViewController.tableView
     myTV?.cellForRow(at: nextIndexPath as IndexPath)?.backgroundColor = UIColor.red
     myTV?.deselectRow(at: nextIndexPath as IndexPath, animated: true)                            
}
...