美文网首页
一个Item自排的算法

一个Item自排的算法

作者: 迷路的安然和无恙 | 来源:发表于2019-02-22 17:13 被阅读0次
    static const NSInteger SortTitleItemColumn = 5;
    static const NSInteger SortTitleRowHeight = 48.0;
    
    - (UIView *)sortView {
        if (!_sortView) {
            UIView *sortView = [[UIView alloc] init];
            sortView.backgroundColor = UPContentBGColor;
            _sortView = sortView;
        
            CGFloat itemSpace = 11.4;
            CGFloat leftMargin = 15;
            CGFloat itemWidth = (UPScreenWidth - leftMargin * 2 - itemSpace * 4) / SortTitleItemColumn;
            NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:3];
            for (int i = 0; i < self.sortTitleArray.count; i ++) {
                UPButton *button = [UPButton buttonWithStyle:UPButtonStyleClickAlpha];
                button.backgroundColor = UPRColorInModule(@"up_market_optional_news_tag_background_color", @"UPMarket");
                [button setTitleColor:UPMajorTextColor forState:UIControlStateNormal];
                [button setTitleColor:UPSelectEnableColor forState:UIControlStateSelected];
                button.titleLabel.font = UPFont(13);
                button.layer.cornerRadius = 12.5;
                button.layer.masksToBounds = YES;
                button.tag = i;
                [button setTitle:self.sortTitleArray[i] forState:UIControlStateNormal];
                if (i == 0) {
                    button.selected = YES;
                }
                [button addTarget:self action:@selector(onTypeButton:) forControlEvents:UIControlEventTouchUpInside];
                [sortView addSubview:button];
                
                int currentRow = floor(i / SortTitleItemColumn);
                [button mas_makeConstraints:^(UPMASConstraintMaker *make) {
                    make.height.equalTo(@25);
                    make.width.equalTo(@(itemWidth));
                    make.top.equalTo(sortView).offset(15 + currentRow * SortTitleRowHeight);
                    int j = i;
                    if (i % SortTitleItemColumn == 0) j = 0;
                    make.left.equalTo(sortView).offset((itemWidth + itemSpace) * j + leftMargin);
                }];
                
                [buttonArray addObject:button];
            }
            _typeButtonArray = [buttonArray copy];
        }
        return _sortView;
    }
    
    - (CGFloat)sortViewHeight {
        CGFloat itemSpace = 11.4;
        CGFloat leftMargin = 15;
        CGFloat itemWidth = (UPScreenWidth - leftMargin * 2 - itemSpace * 4) / SortTitleItemColumn;
        CGFloat oneRowH = SortTitleRowHeight;
        CGFloat itemsW = itemWidth * self.sortTitleArray.count + itemSpace * (self.sortTitleArray.count - 1) + leftMargin * 2;
        NSInteger row = (NSInteger)ceilf(itemsW / UPScreenWidth);
        CGFloat sumH = oneRowH * row;
        return sumH;
    }
    
    - (NSArray *)sortTitleArray {
        if (!_sortTitleArray) {
            if (self.listType == UPNewsListTypeOptionalNews) {
                _sortTitleArray = nil;
            } else if (self.listType == UPNewsListTypeOptionalNotice) {
                _sortTitleArray = @[@"全部", @"利好", @"利空"];
            } else {
                _sortTitleArray = @[@"全部", @"买入", @"增持", @"减持", @"卖出"];
            }
        }
        return _sortTitleArray;
    }
    

    相关文章

      网友评论

          本文标题:一个Item自排的算法

          本文链接:https://www.haomeiwen.com/subject/nfoeyqtx.html