iOS 列表UICollectionView 实现

一个代码:

//
//  SideMenuViewController.m
//  scxhgh
//
//  Created by xmkjsoft on 2024/7/16.
//

#import "SideMenuViewController.h"

@interface SideMenuViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) NSArray *menuItems; // 定义菜单项数组
@property (nonatomic, strong) NSArray *menuIcons; // 定义菜单图标数组
@property (nonatomic, strong) NSArray *messageCounts; // 定义消息数量数组
@end

@implementation SideMenuViewController

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.menuItems.count; // 返回菜单项的数量
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCellIdentifier" forIndexPath:indexPath];
    
    // 配置单元格内容
//    cell.backgroundColor = [UIColor lightGrayColor];
    
      // 配置单元格内容
      UIImageView *menu_icon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
      menu_icon.image = [UIImage imageNamed:self.menuIcons[indexPath.item]]; // 设置菜单图标
      menu_icon.contentMode = UIViewContentModeScaleAspectFill;
      [cell.contentView addSubview:menu_icon];
      
      UILabel *menu_name = [[UILabel alloc] initWithFrame:CGRectMake(35, 5, 100, 30)];
      menu_name.text = self.menuItems[indexPath.item];
      menu_name.textColor = [UIColor blackColor];
//      menu_name.textAlignment = NSTextAlignmentCenter;
      [cell.contentView addSubview:menu_name];
      
      // 消息红点
      NSInteger messageCount = [self.messageCounts[indexPath.item] integerValue];
      if (messageCount > 0) {
          UILabel *badgeLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, 10, 20, 20)];
          badgeLabel.backgroundColor = [UIColor redColor];
          badgeLabel.textColor = [UIColor whiteColor];
          badgeLabel.font = [UIFont systemFontOfSize:10];
          badgeLabel.textAlignment = NSTextAlignmentCenter;
          badgeLabel.text = [NSString stringWithFormat:@"%ld", (long)messageCount];
          badgeLabel.layer.cornerRadius = badgeLabel.frame.size.height / 2;
          badgeLabel.layer.masksToBounds = YES;
          [cell.contentView addSubview:badgeLabel];
      }
        if (messageCount >99) {
            UILabel *badgeLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, 10, 20, 20)];
            badgeLabel.backgroundColor = [UIColor redColor];
            badgeLabel.textColor = [UIColor whiteColor];
            badgeLabel.font = [UIFont systemFontOfSize:10];
            badgeLabel.textAlignment = NSTextAlignmentCenter;
            badgeLabel.text = @"99+";
            badgeLabel.layer.cornerRadius = badgeLabel.frame.size.height / 2;
            badgeLabel.layer.masksToBounds = YES;
            [cell.contentView addSubview:badgeLabel];
        }
      
      // 导向箭头
      UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(260, 10, 10, 20)];
      arrowImageView.image = [UIImage imageNamed:@"arrow"];
      arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
      [cell.contentView addSubview:arrowImageView];
      
 
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *selectedMenuItem = self.menuItems[indexPath.item];
    NSLog(@"Selected menu item: %@", selectedMenuItem);
    
    // 在这里可以根据选择的菜单项执行相应的逻辑
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor]; // 设置背景色为白色或者其他颜色
    
    // 初始化菜单项数据
    // 初始化菜单项数据
      self.menuItems = @[@"我的消息", @"我的动态", @"收藏", @"我的评价", @"优惠券", @"客服", @"设置"]; // 根据实际需要填充菜单项
      
      // 初始化菜单图标数据
      self.menuIcons = @[@"AppIcon", @"AppIcon", @"AppIcon", @"AppIcon", @"AppIcon", @"AppIcon", @"AppIcon"]; // 根据实际需要填充菜单图标
      
      // 初始化消息数量数据
      self.messageCounts = @[@5, @0, @9999, @9999, @9999, @9999, @9999]; // 根据实际需要填充消息数量,这里假设三个菜单项分别有不同的消息数量
    
    //创建用户资料板容器
    UIView *user_info = [[UIView alloc] initWithFrame:CGRectMake(0,0, 300, 100)];
//    user_info.backgroundColor = [UIColor lightGrayColor]; // 使用系统定义的灰色
    // 添加点击事件
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleUserInfoTap:)];
    [user_info addGestureRecognizer:tapGesture];

    [self.view addSubview:user_info];
    
    //    头像图片
    // 创建 UIImageView 并设置图片
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 60, 40, 40)];
    headImageView.image = [UIImage imageNamed:@"AppIcon"]; // 设置头像图片
    headImageView.contentMode = UIViewContentModeScaleAspectFill; // 设置内容模式,填充整个视图

    // 裁剪成圆形
    headImageView.layer.cornerRadius = headImageView.frame.size.width / 2;
    headImageView.clipsToBounds = YES;

    [user_info addSubview:headImageView];
    
    
    // 创建 UILabel
    UILabel *nicknameLabel = [[UILabel alloc] initWithFrame:CGRectMake(55, 60, 200, 40)];
    // 设置昵称文本
    nicknameLabel.text = @"用户131****0392";

    // 设置字体样式和大小
    nicknameLabel.font = [UIFont systemFontOfSize:16.0 weight:UIFontWeightMedium];

    // 设置文本颜色
    nicknameLabel.textColor = [UIColor blackColor];

    // 添加到用户资料板视图中
    [user_info addSubview:nicknameLabel];
    
    
    //导向箭头图标
    // 创建导向箭头图标
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(260, 70, 10, 20)]; // 调整箭头图标位置和尺寸
    arrowImageView.image = [UIImage imageNamed:@"arrow"]; // 设置箭头图标图片
    arrowImageView.contentMode = UIViewContentModeScaleAspectFit; // 设置内容模式,保持图片的长宽比

    // 添加到用户资料板视图中
    [user_info addSubview:arrowImageView];
    
    
    
//    邀请好友下载得积分面板容器
    UIView *invite_friend_panel = [[UIView alloc] initWithFrame:CGRectMake(0,100, 300, 60)];
//    invite_friend_panel.backgroundColor = [UIColor lightGrayColor];
  
    [self.view addSubview:invite_friend_panel];
    
    //邀请按钮
    UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeSystem];
    inviteButton.frame = CGRectMake(10, 10, 280, 40);
    inviteButton.backgroundColor = [UIColor colorWithRed:(255/255.0) green:(111/255.0) blue:(97/255.0) alpha:1.0];
    inviteButton.layer.cornerRadius = 20.0;
    inviteButton.layer.borderWidth = 1.0;
    inviteButton.layer.borderColor = [UIColor colorWithRed:(255/255.0) green:(111/255.0) blue:(97/255.0) alpha:1.0].CGColor;
    [inviteButton setTitle:@"邀请好友下载得积分" forState:UIControlStateNormal];
    [inviteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [inviteButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
    // 添加点击事件
    [inviteButton addTarget:self action:@selector(inviteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

    // 设置按钮文字和图标的对齐方式
    inviteButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    inviteButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); // 调整文字与按钮边界的间距

    // 添加右边的图标
    UIImage *iconImage = [UIImage imageNamed:@"download"]; // 替换为你的图标图片
    UIImageView *iconImageView = [[UIImageView alloc] initWithImage:iconImage];
    iconImageView.frame = CGRectMake(CGRectGetWidth(inviteButton.frame) - 40, 10, 20, 20); // 调整图标位置和大小
    [inviteButton addSubview:iconImageView];
    

    [invite_friend_panel addSubview:inviteButton];
    
    
    //菜单列表
       UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
       layout.scrollDirection = UICollectionViewScrollDirectionVertical;
       layout.itemSize = CGSizeMake(280, 40); // 每个单元格的尺寸
       layout.minimumInteritemSpacing = CGFLOAT_MIN; // 最小间距设为最小值,确保每个单元格单独一行
       
       UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 160, 280, CGRectGetHeight(self.view.bounds) - 160) collectionViewLayout:layout];
       collectionView.backgroundColor = [UIColor whiteColor];
       collectionView.dataSource = self;
       collectionView.delegate = self;
       [self.view addSubview:collectionView];
       [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MenuCellIdentifier"];


   

}


//邀请好友的点击事件
- (void)inviteButtonTapped:(UIButton *)sender {
    NSLog(@"Invite button tapped!");
    // 在这里添加处理按钮点击后的逻辑
    // 例如,处理按钮点击后的跳转、数据操作等
}


//用户资料板 点击事件处理方法
- (void)handleUserInfoTap:(UITapGestureRecognizer *)sender {
    // 在这里处理点击事件,例如打开用户详细信息页面
    NSLog(@"用户资料板被点击,点击跳转主页");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值