美文网首页
iOS SelectTap

iOS SelectTap

作者: yejinghun | 来源:发表于2017-12-26 11:08 被阅读0次

本人新人一枚,在之前的工作当中有一个功能需求点,做一个标签页面点点击以及返回点击的标签内容,所以就找时间重新写了下这个页面。

首先我的想法就是用tableView去创建两个section,第一个section去显示选中的标签字段,第二个section去显示一开始所传过来的标签数组,然后根据计算每个标签的宽度去实现高度的自动变化。

-(CGFloat)showLabelViewWithHeight:(NSArray *)array{
    
    _rowCount=_lastWidth=0;
    for (int i=0; i<array.count; i++) {
        
        if (i==0) {
            
            _lastWidth=kWPercentage(10)+kWPercentage(10)+[self getWidthWithTitle:array[i] font:self.titleFont];
            
        }else{
            
            if (_lastWidth+kWPercentage(20)+[self getWidthWithTitle:array[i] font:self.titleFont]+kWPercentage(10)>self.frame.size.width) {
                
                _lastWidth=0;
                _rowCount+=1;
                _lastWidth+=kWPercentage(10)+kWPercentage(10)+[self getWidthWithTitle:array[i] font:self.titleFont];
                
            }else{
                
                _lastWidth+=kWPercentage(10)+kWPercentage(10)+[self getWidthWithTitle:array[i] font:self.titleFont];
                
            }
            
        }
        
    }
    return kHPercentage(10)+kHPercentage(27)*(_rowCount+1);
}

然后在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中添加标签的方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"selectLabel"];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.section==0) {
        [cell addSubview:[self showSelectViewArray:self.selectNSArray]];
    }else{
        [cell addSubview:[self showSelectViewArray:self.allNSArray]];
    }
    return cell;
    
}

-(UIView *)showSelectViewArray:(NSArray *)array{
    
    self.selectView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, [self showLabelViewWithHeight:array])];
    _rowCount=_lastWidth=0;
    [self showLabel:array];
    return self.selectView;
    
}

-(void)showLabel:(NSArray *)array{
    
    for (int i=0; i<array.count; i++) {
        
        if (i==0) {
            
            _label=[[UILabel alloc]initWithFrame:CGRectMake(kWPercentage(10), kHPercentage(10), kWPercentage(10)+[self getWidthWithTitle:array[i] font:[UIFont systemFontOfSize:kHPercentage(13)]], kHPercentage(17))];
            _lastWidth=kWPercentage(10)+kWPercentage(10)+[self getWidthWithTitle:array[i] font:self.titleFont];
            
        }else{
            
            if (_lastWidth+kWPercentage(20)+[self getWidthWithTitle:array[i] font:self.titleFont]+kWPercentage(10)>self.frame.size.width) {
                
                _lastWidth=0;
                _rowCount+=1;
                _label=[[UILabel alloc]initWithFrame:CGRectMake(kWPercentage(10), kHPercentage(10)+kHPercentage(27)*_rowCount, kWPercentage(10)+[self getWidthWithTitle:array[i] font:self.titleFont], kHPercentage(17))];
                _lastWidth+=kWPercentage(10)+kWPercentage(10)+[self getWidthWithTitle:array[i] font:self.titleFont];
                
            }else{
                
                _label=[[UILabel alloc]initWithFrame:CGRectMake(kWPercentage(10)+_lastWidth, kHPercentage(10)+kHPercentage(27)*_rowCount, kWPercentage(10)+[self getWidthWithTitle:array[i] font:self.titleFont], kHPercentage(17))];
                _lastWidth+=kWPercentage(10)+kWPercentage(10)+[self getWidthWithTitle:array[i] font:self.titleFont];
                
            }
            
        }
        _label.text=array[I];
        _label.textAlignment=NSTextAlignmentCenter;
        _label.font=self.titleFont;
        _label.layer.borderWidth=1;
        _label.layer.borderColor=[UIColor redColor].CGColor;
        _label.layer.cornerRadius=2;
        _label.clipsToBounds=YES;
        [_label addGestureRecognizer:tap];
        [self.selectView addSubview:_label];
        
    }
}

最后就是给label添加点击事件,当点击所有标签中的label时,将其移除所有标签的NSMutableArray并添加到我的标签的NSMutableArray中,并刷新整个tableView。

-(void)clickLabel:(UITapGestureRecognizer *)sender{
    
    if (sender.view.tag/10000>=1) {
        [self.selectNSArray addObject:self.allNSArray[sender.view.tag%10000]];
        [self.allNSArray removeObjectAtIndex:(sender.view.tag%10000)];
        [self.tableView reloadData];
    }else{
        [self.allNSArray addObject:self.selectNSArray[sender.view.tag%100]];
        [self.selectNSArray removeObjectAtIndex:(sender.view.tag%100)];
        [self.tableView reloadData];
    }
    
}

效果图如下


Simulator Screen Shot - iPhone X - 2017-12-26 at 10.47.18.png Simulator Screen Shot - iPhone X - 2017-12-26 at 10.47.28.png

附带上gitHub地址:https://github.com/yejinghun/SelectTap。觉得还行就帮忙点下小🌟,如果有什么好的建议,请联系我。2746345626@qq.com

相关文章

网友评论

      本文标题:iOS SelectTap

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