UITableView布局的简单使用

ZWalletViewSetting.h文件

//
//  ZWalletViewSetting.h
//  SalesiPhone
//

#import <Foundation/Foundation.h>
#import "WYSuperViewController.h"

NS_ASSUME_NONNULL_BEGIN

@interface ZWalletViewSetting : WYSuperViewController

@end

NS_ASSUME_NONNULL_END

ZWalletViewSetting.m文件


#import "ZWalletViewSetting.h"
#import "ZWalletViewCell.h"
#import "ZMyAccountView.h"
#import "ZBindCodeView.h"


@interface ZWalletViewSetting ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic)UITableView*tableView;


@property(nonatomic)NSMutableArray*dataArray;
    
@end

@implementation ZWalletViewSetting

-(void)viewDidLoad{
    [super viewDidLoad];
    _dataArray=[[NSMutableArray alloc] initWithObjects:@"我的账户",@"我的邀请码",nil];
    [self.view addSubview:self.tableView];
}

-(void)initNormalTitleNavBarSubviews{
    [self setTitle:@"我的钱包"];
    return;
}


-(UITableView*)tableView{
    if(!_tableView){
     // UITableViewStylePlain会使头部悬浮
     // UITableViewStyleGrouped cell分组
        _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, NavigationHeight, SCREEN_WIDTH, SCREENH_HEIGHT-NavigationHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //设置每行的分割线
//      _tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
        //不显示分割线
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark UITableViewDelegate,UITableViewDataSource
//返回有多少个sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//每个sections 有多少行row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}
//每个sections的头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
//每个sections的表头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

//每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT_VALUE(40);
}

//每行的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString * walletViewCell = @"walletViewCellId";
        ZWalletViewCell * cell = [tableView dequeueReusableCellWithIdentifier:walletViewCell];
        if(!cell){
            cell = [[ZWalletViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:walletViewCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;//cell选中状态
        }
        [cell setDTO:[_dataArray objectAtIndex:indexPath.row]];
        return cell;
 }

//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0:{
            //我的账户
            ZMyAccountView* Vc = [[ZMyAccountView alloc] init];
            [self.navigationController pushViewController:Vc animated:NO];
           }
            break;
        case 1:{
           ZBindCodeView* Vc= [[ZBindCodeView alloc] init];
           [self.navigationController pushViewController:Vc animated:NO];
         }
            break;
            
        default:
            break;
    }
}

@end

ZWalletViewCell.h文件

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ZWalletViewCell : UITableViewCell

- (void)setDTO:(NSString *)title;

@end

NS_ASSUME_NONNULL_END

ZWalletViewCell.m文件

//
//  ZWalletViewCell.m
//  SalesiPhone
//
//  Created by ztaotech005 on 2020/1/6.
//

#import "ZWalletViewCell.h"
@interface ZWalletViewCell ()

@property(nonatomic)UILabel*lableTitle;
@property(nonatomic)UILabel*line;
@property(nonatomic)UIImageView*iconImg;

@end

@implementation ZWalletViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.lableTitle];
        [self addSubview:self.line];
        [self addSubview:self.iconImg];
        
    }
    return self;
}

-(UILabel*)lableTitle{
    if(!_lableTitle){
        _lableTitle=[[UILabel alloc] initWithFrame:CGRectMake(WIDTH_VALUE(20), 0, SCREEN_WIDTH-WIDTH_VALUE(40), HEIGHT_VALUE(39))];
        _lableTitle.font=[UIFont systemFontOfSize:16];
    }
    return _lableTitle;
}
-(UILabel*)line{
    if(!_line){
        _line=[[UILabel alloc] initWithFrame:CGRectMake(0, _lableTitle.bottom, SCREEN_WIDTH, 1)];
        _line.backgroundColor=HEXCOLOR(0xE6E6E6);
    }
    return _line;
}
-(UIImageView*)iconImg{
    if(!_iconImg){
        _iconImg=[[UIImageView alloc] initWithFrame:CGRectMake(_lableTitle.right,HEIGHT_VALUE(15), HEIGHT_VALUE(10), HEIGHT_VALUE(10))];
        _iconImg.image=[UIImage imageNamed:@"cell_arrow"];
    }
    return _iconImg;
}

- (void)setDTO:(NSString *)title{
    _lableTitle.text=title;
}
@end

效果图如下
在这里插入图片描述

还可以添加左滑删除

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    //商品下架
        return @"删除";
   
}
//点击删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值