iOS入门-31UITableView基础

概述

UITableView:
  • 用来实现列表的系统View(类似于Android中的ListView,Flutter中的ListView)
  • 由于是系统自带View那么功能肯定是有限的,但是从使用的过程中可以了解实现原理和大概的代码风格
  • 自带功能有哪些(重要的是那些要实现的代理协议)
基础篇为介绍
  • cell高度
  • 每个section的headerview和footerview的高度
  • cell的创建,复用
  • UITableView自带头部和底部View;
  • UITableView先分组section,每个section中又分row,一行行

示例

先看图

在这里插入图片描述

示例代码

AppDelegate.h
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property(retain,nonatomic) UIWindow* window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    ViewController* vc = [ViewController new];
    
    UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:vc];
    
    self.window.rootViewController = navC;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

@end
ViewController.h
#import <UIKit/UIKit.h>

//实现两个代理UITableViewDelegate,UITableViewDataSource
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    //声明一个UITableView对象
    UITableView* _tableView;
    //数据源
    NSMutableArray* _arrayData;
}
@end
ViewController.m
#import "ViewController.h"
#import "VCSecond.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"UITableViewDemo";
    //创建UITableView,并设置两个代理对象
    _tableView = [UITableView new];
    //大小为屏幕大小
    _tableView.frame = [UIScreen mainScreen].bounds;
    //代理为视图控制器对象
    _tableView.delegate = self;
    //数据代理为视图控制器对象
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    //生成一个二维数组(数据源)
    _arrayData = [NSMutableArray new];
    for (int i='A'; i<='Z'; i++) {
        NSMutableArray* array = [NSMutableArray new];
        
        for (int j=1; j<=5; j++) {
            NSString* str = [NSString stringWithFormat:@"%C%D",i,j];
            [array addObject:str];
        }
        
        [_arrayData addObject:array];
    }
}

//获取组数(UITableView先分组section,组里分元素row。每一个条目为一个元素)
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return _arrayData.count;
}

//每组中元素个数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[_arrayData objectAtIndex:section] count];
}

//单元格
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* str = @"cell";
    //试图获取cell
    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:str];
    //cell为空则创建
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    //单元格中title赋值
    cell.textLabel.text = _arrayData[indexPath.section][indexPath.row];
    return cell;
}

//section头部title
-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"头部";
}

//section尾部title
-(NSString*) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"尾部";
}

//row高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

//section头部高度
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 80;
}

//section尾部高度
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值