3

我有一个我放进去的NSMutableArray东西,负责把它们整理好。现在我有这个代码可以改变两个位置:UIViewsUIScrollViewUIViews

UIView *viewToBeChanged = [views objectAtIndex:self.page];
UIView *previousView = [views objectAtIndex:previousPage];

[views removeObjectAtIndex:self.page];
[views removeObjectAtIndex:previousPage];

[views insertObject:viewToBeChanged atIndex:previousPage];
[views insertObject:previousView atIndex:self.page];

有没有更好的方法来改变NSMutableArray另一个对象的位置?谢谢!

4

1 回答 1

5

您可以使用replaceObjectAtIndex:i方法

 [array replaceObjectAtIndex:i withObject:newObj];

您还可以使用exchangeObjectAtIndex:withObjectAtIndex

  [array exchangeObjectAtIndex:i withObjectAtIndex:j];
于 2013-08-19T13:25:24.457 回答