zoukankan      html  css  js  c++  java
  • 根据数组添加 按钮及文本,按钮可点击

    #import <UIKit/UIKit.h>

    //自定义头文件

    @interface CustomView : UIView

    -(id)initWithFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray;

    -(id)initWithFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray textArray:(NSMutableArray *)textArray;

    @end

    #import "CustomView.h"

    //自定义实现文件

    @implementation CustomView

    -(id)initWithFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray

    {

        self = [super initWithFrame:frame];

        if (self)

        {

            //button坐标

            float bx = 0;

            float by = 0;

            float bw = frame.size.width/4;

            float bh = bw;

            

            //imageView坐标

            float imgX = 10;

            float imgW = bw-20;

            float imgH = imgW;

            float imgY = 10;

            

            for (int i = 0; i < dataArray.count; i++)

            {

            

                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    //            button.backgroundColor = [UIColor yellowColor];

                

                if (i % 4 == 0 && i > 0)

                {

                    bx = 0;

                    by += bh;

                }

                button.frame = CGRectMake(bx, by, bw, bh);

                UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(imgX, imgY, imgW, imgH)];

    //            imgView.backgroundColor = [UIColor redColor];

                imgView.image = [UIImage imageNamed:@"1.png"];

                [button addSubview:imgView];

                [self addSubview:button];

                bx += bw;

            }

            

        }

        return self;

    }

    -(id)initWithFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray textArray:(NSMutableArray *)textArray

    {

        self = [super initWithFrame:frame];

        if (self)

        {

            //button坐标

            float bx = 0;

            float by = 0;

            float bw = frame.size.width/4;

            float bh = bw;

            

            //imageView坐标

            float imgX = 15;

            float imgW = bw-30;

            float imgH = imgW;

            float imgY = 15;

            

            float selfHeight = 0.0;

            

            for (int i = 0; i < dataArray.count; i++)

            {

                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

                button.tag = i+100;

                

                if (i % 4 == 0 && i > 0)

                {

                    bx = 0;

                    by += bh;

                }

                

                button.frame = CGRectMake(bx, by, bw, bh);

                [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

                [self addSubview:button];

                

                UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(imgX, imgY, imgW, imgH)];

                imgView.layer.cornerRadius = imgW/2;

                imgView.layer.masksToBounds = YES;

                imgView.image = [UIImage imageNamed:@"1.png"];

                [button addSubview:imgView];

                

                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(imgX, imgY+imgH+3, imgW, 20)];

                label.text = [textArray objectAtIndex:i];

                label.font = [UIFont systemFontOfSize:14.0];

                label.textColor = [UIColor colorWithRed:65/255.0 green:65/255.0 blue:65/255.0 alpha:1];

                label.textAlignment = NSTextAlignmentCenter;

                [button addSubview:label];

                bx += bw;

                selfHeight = by+bh;

            }

            

            CGRect selfFrame = self.frame;

            selfFrame.size.height = selfHeight;

            self.frame = selfFrame;

        }

        return self;

    }

    -(void)buttonClick:(UIButton *)btn

    {

        NSUInteger index  = btn.tag - 100;

        NSString *str = [NSString stringWithFormat:@"%lu",(unsigned long)index];

        NSLog(@"第%@ ------button",str);

        

    }

    运行效果如下图

  • 相关阅读:
    图片上传记得要讲中文的重命名
    hihoCoder #1471 拥堵的城市
    搜狗拼音输入法的快捷键和其他应用快捷键冲突
    Codeforces #765D
    训练记录
    hihoCoder 1367 等式填空
    hihoCoder #1073 光棍节
    计算几何 I. 极角
    hihoCoder #1065 全图传送
    树的点分治专题
  • 原文地址:https://www.cnblogs.com/camillezlh/p/4576496.html
Copyright © 2011-2022 走看看