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);

        

    }

    运行效果如下图

  • 相关阅读:
    ABP 往前端返回详细的错误信息
    ABP 报错1
    three.js 测试1
    three.js 添加 图形控制界面 gui
    three.js 设置透明度
    three.js 基础使用1
    three.js 添加环境光
    three.js 添加三维坐标系
    P2690 接苹果
    [USACO08FEB]修路Making the Grade
  • 原文地址:https://www.cnblogs.com/camillezlh/p/4576496.html
Copyright © 2011-2022 走看看