加字母的逻辑有的 绕弯,

-(void)setCityBgcolor:(int)row indexLetter:(NSString *)letter 

{

   

    if (row != frontRow || (row == 0 && !rowZeroOneTime)) {

        

        UIImageView *citylistBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"citylist_bg.png"]];

        citylistBgView.frame = CGRectMake(0, originY, 320, 42);

        [self.cityScrollView addSubview:citylistBgView];

        citylistBgView.tag = citylistBgViewtag;

        citylistBgViewtag++;

        

        cityIndexImageView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"city_index_bg.png"]];

        cityIndexImageView.frame = CGRectMake(0, row*41-1, 14, 42);

        cityIndexImageView.backgroundColor = [UIColorclearColor];

        if (![frontLetter isEqualToString:letter]) {

            UILabel *label = [[UILabel alloc] init];

            [label setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];

            label.frame = CGRectMake(2, 0, 14, 42);

            label.backgroundColor = [UIColor clearColor];

            label.text = letter;

            label.textColor = [UIColor whiteColor];

            [cityIndexImageView addSubview:label];

        }

 

        cityIndexImageView.tag = imageIndexViewTag;

        imageIndexViewTag ++;

        [self.cityScrollViewaddSubview:cityIndexImageView];

        

        rowZeroOneTime = YES;

        originY = originY+41;

        frontLetter = letter;

    }

    

    frontRow = row;

 

}

原文地址:https://www.cnblogs.com/guligei/p/3025879.html