-(IBAction)indexCityView:(id)sender
{
UIButton *button = (UIButton *)sender;
int btnTag = button.tag;
NSString *stopLetter;
NSString *containLetter;
switch (btnTag) {
case 0:
containLetter = @"ABCDE";
stopLetter = @"F";
break;
case 1:
containLetter = @"FJHIJ";
stopLetter = @"K";
break;
case 2:
containLetter = @"KLMNO";
stopLetter = @"P";
break;
case 3:
containLetter = @"PQRST";
stopLetter = @"U";
break;
default:
containLetter = @"UVWXYZ";
stopLetter = @"a";//ascII a:97, Z:90
break;
}
int rampage;//行数
int column;//列数
int buttonViewTag = 1110 ;//移出scrollview上面的 button,设置tag的起始值
for (int i =buttonViewTag; i<1110 + self.cityList.count; i++) {
[[self.viewviewWithTag:i] removeFromSuperview];
}
for (int i = 0; i<self.cityList.count ; i++) {
BOOL jumpOutsideCircle = NO;
HFCity *hfCity = (HFCity *)[self.cityList objectAtIndex:i];
NSString *cStartLetter = hfCity.cityStartLetter;//获得起始字母
BOOL isContainLetter = NO;
NSRange range=[containLetter rangeOfString:cStartLetter];
if(range.location!=NSNotFound){
isContainLetter = YES;
}
NSString *nowLetter;
NSString *nextLetter;
int nextLine;
BOOL nextLineFlag;
if (isContainLetter) {
for (int j = i; j<self.cityList.count; j++) {
jumpOutsideCircle = YES;//跳出外面for循环标志
hfCity = (HFCity *)[self.cityList objectAtIndex:j];
NSComparisonResult result = [hfCity.cityStartLetter compare:stopLetter];
BOOL compareResult = result == NSOrderedDescending ? YES:NO;
if ([hfCity.cityStartLetter isEqualToString:stopLetter] || compareResult == YES ) {
break; //跳出内部for循环
}
nextLetter = hfCity.cityName;
if (nextLineFlag) {
nextLine++;
}else{
nextLine = j;
}
//进一步剥离 abcde
if (![nowLetter isEqualToString:nextLetter]) {
nextLine = j+4-column;
nextLineFlag = YES;
}
UIButton *btn = [[UIButton alloc] init];
[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:hfCity.cityNameforState:UIControlStateNormal];
[btn setTitleColor:[UIColorcolorWithRed:128/255green:128/255blue:128/255alpha:1] forState:UIControlStateNormal];
[btn setTitleColor:[UIColorcolorWithRed:0/255.0green:132/255.0blue:26/255.0alpha:1] forState:UIControlStateHighlighted];
[btn addTarget:selfaction:@selector(chooseCity:) forControlEvents:UIControlEventTouchUpInside];
buttonViewTag++;
btn.tag = buttonViewTag;
rampage = (nextLine - i)/4;//行5/4=1,5%4=1,4%5=4
column = (nextLine - i)%4;//列
int x = column*70;
int y = rampage*30;
btn.frame = CGRectMake(x, y, 70, 30);
[self.cityScrollView addSubview:btn];
nowLetter = hfCity.cityName;
}
if (jumpOutsideCircle) {
break;//跳出外部for循环
}
}
}
self.cityScrollView.contentSize = CGSizeMake(65*column, 50*rampage);//width,height
self.cityScrollView.tag = 002;
}