我有一个数据情况,我想使用索引路径。当我遍历数据时,我想增加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);
这感觉有点,嗯,笨重 - 有更好的方法吗?
答案 0 :(得分:6)
你可以试试这个 - 也许同样笨重,但至少要短一点:
NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];
答案 1 :(得分:5)
这样一行:
indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:actualIndexPath.section];
答案 2 :(得分:3)
在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
}
答案 3 :(得分:1)
Swift 4中的for循环使用嵌入式UITableView获得相似的结果,遍历for循环,并用“行更新”填充单元格的详细文本
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)
}