感觉这个比较好用一些,先写一下它怎么用吧
//代表是从0-count之间的随机数
int r = arc4random()%count
//代表从x---x+y之间的随机数
int r = arc4random() %x +y;
下面举个列子吧
NSArray *array = [[NSArrayalloc] initWithObjects:@"0",@"1 ",@"2",@"3",@"4",@"5",@"6",@"7",nil];
NSMutableArray *randomArray = [[NSMutableArrayalloc] init];
while ([randomArray count] < array.count) {
int r = arc4random() %array.count;
[randomArray addObject:[array objectAtIndex:r]];
}
NSLog(@"%@",randomArray);
输出结果是这样的
(
7,
4,
2,
2,
0,
6,
2,
4
)
可以看到每次取值都是随机数,所以取得这8次有时候会取到重复的值
--------下面是取出不重复的随机数------
//随机数从这里边产生
NSMutableArray *startArray=[[NSMutableArrayalloc] initWithObjects:@0,@1,@2,@3,@4,@5,@6,@7,nil];
//随机数产生结果
NSMutableArray *resultArray=[NSMutableArrayarray];
//随机数个数
NSInteger m=8;
for (int i=0; i<m; i++) {
int t=arc4random()%startArray.count;
resultArray[i]=startArray[t];
//移除已经出现的数值
[startArray removeObjectAtIndex:t];
}
NSLog(@"%@",resultArray);
打印结果
(
6,
7,
4,
0,
2,
3,
5,
1
)
// 去掉UItableview headerview黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat sectionHeaderHeight = 100;
if ( (scrollView.contentOffset.y <= sectionHeaderHeight) && (scrollView.contentOffset.y >= 0) )
{
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
}
else if (scrollView.contentOffset.y >= sectionHeaderHeight)
{
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}