ios 團購分類頁面(9宮格)

来源:http://www.cnblogs.com/shouce/archive/2016/05/31/5544654.html
-Advertisement-
Play Games

命名有些錯誤,但功能實現,以後註意下命名規範 WJViewGroup.h #import <UIKit/UIKit.h> @interface WJViewGroup : UIView { NSInteger _width; NSInteger _height; } @property (nonat ...


=-= 命名有些錯誤,但功能實現,以後註意下命名規範

WJViewGroup.h

複製代碼
#import <UIKit/UIKit.h>

@interface WJViewGroup : UIView {
    NSInteger _width;
    NSInteger _height;
}

@property (nonatomic,assign)NSInteger widthNum; //橫排多少個
@property (nonatomic,assign)NSInteger heightNum; //豎排多少個

@property (copy, nonatomic) void(^itemBlock)(NSInteger index);//按鈕點擊回調
@property (nonatomic,assign)BOOL isLine;
@property (nonatomic,strong)NSMutableArray *array;

/**
 *  添加item
 *
 *  @param widthItem  高數量
 *  @param heightItem 寬數量
 *  @param num        item數量
 */
- (void)addItem:(NSInteger)widthItem widthItem:(NSInteger)heightItem num:(NSArray *)num;
/**
 *  改變item的文字
 *
 *  @param array 文字數組
 */
- (void)item:(NSArray *)array;
@end
複製代碼

WJViewGroup.m

複製代碼
#import "WJViewGroup.h"

@implementation WJViewGroup

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _width = frame.size.width;
        self.backgroundColor = [UIColor whiteColor];
        _height = frame.size.height;
        self.layer.borderWidth = 1;
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 5;
        _widthNum = 3;
        _heightNum = 3;
        _array = [[NSMutableArray alloc]init];
    }
    return self;
}

#pragma mark - 添加item 3行---------

- (void)addItem:(NSInteger)widthItem widthItem:(NSInteger)heightItem num:(NSArray *)num{
    NSInteger index = 0;
    NSInteger indeNume = 0;
    for (NSInteger i = 0; i < widthItem; i ++) {
         NSInteger index_Y = 0;
        for (NSInteger j = 0; j < heightItem; j++) {
         
            if (indeNume < num.count) {
                UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(_width/_widthNum*index_Y, _height/_heightNum * index, _width/_widthNum, _height/_heightNum)];
                [button setTitle:num[indeNume] forState:UIControlStateNormal];
                button.tag = indeNume;
                button.titleLabel.font = [UIFont systemFontOfSize:14];
                [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:button];
                [_array addObject:button];
                if (_isLine && index_Y < 2) {
                    UIView *lineVertical  = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(button.frame), CGRectGetMinY(button.frame) + _height/_heightNum *0.2, 0.5, _height/_heightNum *0.6)];
                    lineVertical.backgroundColor = [UIColor lightGrayColor];
                    [self addSubview:lineVertical];
                }
                
            }
            indeNume ++;
            index_Y++;
        }
       
            index++;
    }
    if (_isLine) {
        [self addCrossLine:0];
    }
 
}

- (void)item:(NSArray *)array {
    NSInteger index = 0;
    for (UIButton *button in _array) {
        if (index < array.count) {
             [button setTitle:array[index] forState:UIControlStateNormal];
        }
        index++;
    }
}

#pragma mark - 添加橫豎線

- (void)addCrossLine:(NSInteger)index {
    if (index == 0) {
        for (int i = 0; i < 3; i ++) {
            if (i > 0) {
                UIView *crossLine = [[UIView alloc]initWithFrame:CGRectMake(_width *0.05, _height/_heightNum*i, _width *0.9, 0.5)];
                crossLine.backgroundColor = [UIColor lightGrayColor];
                [self addSubview:crossLine];
            }
 
        }
    }else {
        for (int i = 0; i < 3; i ++) {
            if (i > 0) {
                UIView *crossLine = [[UIView alloc]initWithFrame:CGRectMake(0, _height/_heightNum*i, _width, 0.5)];
                crossLine.backgroundColor = [UIColor lightGrayColor];
                [self addSubview:crossLine];
            }
        }
    }
}

- (void)addVerticalLine {
    for (int i = 0; i < 3; i ++) {
        if (i < 2) {
            UIView *verticalLine = [[UIView alloc]initWithFrame:CGRectMake(_width/_widthNum * (i+1),0, _width, 0.5)];
            verticalLine.backgroundColor = [UIColor grayColor];
            [self addSubview:verticalLine];
        }
    }
}

#pragma mark - 事件監聽

- (void)buttonPressed:(UIButton *)sender {
    if (sender.titleLabel.text.length > 0) {
        if (_itemBlock) {
            _itemBlock(sender.tag);
        }

    }
}

@end
複製代碼

使用:

1.直接使用

複製代碼
  WJViewGroup *group = [[WJViewGroup alloc]initWithFrame:CGRectMake(ScreenWidth*0.05, 20, ScreenWidth*0.9, 120)];
    group.isLine = YES;

    group.layer.borderColor = [UIColor colorWithHexString:@"d3d1d1"].CGColor;
    [group addItem:3 widthItem:3 num:@[@"美食",@"電影",@"KTV",@"龍蝦",@"甜品",@"海鮮",@"火鍋",@"自助餐",@"蔬菜"]];
    [group setItemBlock:^(NSInteger index) {
        NSLog(@"%ld",index);
        
    }];
    [self.view addSubview:group];
複製代碼

效果圖:

2.cell中使用

自定義初始化cell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellHeight:(NSInteger)cellHeight; 
複製代碼
      _group = [[WJViewGroup alloc]initWithFrame:CGRectMake(ScreenWidth*0.05, CGRectGetHeight(viewBG.frame) +5, ScreenWidth*0.9, cellHeight)];
        _group.isLine = YES;
        if (cellHeight == 80) {
            _group.heightNum = 2;
        }
        _group.layer.borderColor = [UIColor colorWithHexString:@"d3d1d1"].CGColor;
        [_group addItem:3 widthItem:3 num:@[@"",@"",@"",@"",@"",@"",@"",@"",@""]];
        [self.contentView addSubview:_group];
複製代碼

懶載入:

複製代碼
- (NSArray *)arrayContext {
    if (!_arrayContext) {
        _arrayContext = [[NSArray alloc]initWithObjects:
  @[@"火鍋",@"蛋糕甜點",@"西餐",@"自助餐",@"小吃快餐",@"香鍋烤魚",@"聚餐宴請",@"燒烤烤肉",@"日韓料理"],
  @[@"經濟型酒店",@"豪華酒店",@"主題酒店",@"公寓型酒店",@"客棧",@"青年旅社"],
  @[@"美髮",@"美甲",@"美容美體",@"瑜伽/舞蹈"],
  @[@"溫泉",@"海洋館",@"展覽館",@"動植物園",@"主題公園",@"水上樂園"],
  @[@"演出賽事",@"桌游/電玩",@"運動健身",@"足療按摩",@"洗浴/汗蒸",@"4D/5D電影",],
                         nil];
    }
    return _arrayContext;
}
複製代碼

代理方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    [cell.group item:self.arrayContext[indexPath.row]];
    [cell.group setItemBlock:^(NSInteger index) {
        NSLog(@"%ld",index);
        
    }];

效果圖:

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 現在開始寫博客,分享android開發中的心得。 ...
  • 運行結果列印: 原文文檔介紹: Runs the loop once, blocking for input in the specified mode until a given date. //執行loop一次,堵塞等待給定模式的輸入直至給定的時間點 Parameters mode The mo ...
  • 做基於WebView應用時,頁面上有一個輸入框,當輸入的文字過多時,超過輸入框的行數時,輸入框能夠滾動,這時間問題來了,輸入的提示箭頭會移動到輸入框外,如何解決這個問題呢,查找chromium源碼如下: void LoadIfNecessary(jobject context) { if (load ...
  • 1.代碼如下 (註釋都有) - (void)viewDidLoad { [super viewDidLoad]; UIImageView * bigImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100) ...
  • 複製並修改原有項目 複製之前創建的項目CC+CV操作 需要改動的地方: * 項目名字 * 應用包名 * R文件重新導包 接著修改件/AndroidManifest.xml中的包名:package="com.wuyudong.rwinrom" 簡單起見直接改為package="com.wuyudong ...
  • ~/Library/MobileDevice/Provisioning Profiles ...
  • 蘋果的通訊錄功能在iOS7,iOS8,iOS9 都有著一定的不同,iOS7和8用的是 <AddressBookUI/AddressBookUI.h> ,但是兩個系統版本的代理方法有一些變化,有些代理方法都標註了 NS_DEPRECATED_IOS(2_0, 8_0) 並推薦了另一個代理方法與之對應。 ...
  • 一,效果圖。 二,工程圖。 三,代碼。 RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UITableViewDataSource,UITableViewDe ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...