我有一个NSMutableArray排序对象,它们显示在UITableView中。
我想在数组中插入一个新对象并更新表视图 - 这需要新插入对象的索引。
我找不到任何系统消息告诉我正确的数组插入索引,我需要更新表视图。
我能找到的最好的是:
或
当然,必须有消息才能在排序数组中找到插入位置?或者我错过了一些明显的东西?
答案 0 :(得分:25)
您可以在整个阵列上使用indexOfObject:inSortedRange:options:usingComparator:
方法。此方法对您传递的范围执行二进制搜索,并在您使用NSBinarySearchingInsertionIndex
选项时为您提供插入点:
NSUInteger insPoint = [myArray
indexOfObject:toInsert
inSortedRange:NSMakeRange(0, [myArray count])
options:NSBinarySearchingInsertionIndex
usingComparator:^(id lhs, id rhs) {
return // return the result of comparing two objects
}
];