【UI初级--连载八】-------------表视图(UITableView)

本文详细介绍了如何使用Storyboard创建并配置UITableView,包括不同场景下的TableView使用步骤、注意事项、属性与方法详解,以及如何实现代理与数据源委托方法。内容涵盖了TableView的基本创建、属性配置、方法使用,以及如何在不同导航控制器场景下正确配置TableView,同时提供了TableView的数据源与代理委托方法的实战应用案例。
1、总结一:storyBoard使用步骤及注意事项(注册单元格)
2、总结二:tableView属性和方法
3、总结三:tableView常用数据源委托方法(dataSource)
4、总结四:tableView常用代理委托方法(delegate)

总结一:使用storyBoard
(注:_tableView.dataSource = self;
        _tableView.delegate = self; (在storyBoard中连线))

1、不需要NavigationController
在storyBoard中:拖个TableView至viewController,托代理,拖DataSource

2、需要NavigationController
在storyBoard中:
(1)拖NavigationController(删掉自带的RootViewController),is(入口)
(2)连线NavigationController和viewController(Root)
(3)拖个tableView至viewController(还需托代理)
(4)在.m文件中实现代理方法
(4.1)返回单元格方法中(单元格复用)
(4.2)拖table View cell (会有Prototype cells),
此时返回单元格方法中(单元格复用 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: forIndexPath: ];
(4.2.1)(xib)中,选中cell, 设置identifier
(4.2.2)在.m文件中 viewDidLoad方法中,在注册一个单元格(UI07-06)
( //注册一个单元格 (通过类去注册)---(不建议)
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];)
(5)如果从别的ViewController弹出本视图控制器的话,弹出时需用到以下方法:

//拿到storyboard当中创建的控制器对象
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
           
        NewsDetailViewController *detailVC = [storyboard instantiateViewControllerWithIdentifier:@"NewsDetailVC"];
           
        //push进入图片详情界面
        [self.navigationController pushViewController:detailVC animated:YES];




总结二:tableView属性和方法
【1】创建
    UITableView *tableView = [[UITableView alloc] initWithFrame:  style: ];
   //frame设置方法一:
   CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20)
   
    //frame设置方法二:
    self.view.bounds
    //设置tableView的内容偏移量
   tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);

【2】属性(tableView的属性)
《1》分割线
(1)分割线风格---------------------separatorStyle
(2)分割线颜色---------------------separatorColor,默认标准灰色
(3)分割线偏移量-------------------separatorInset (上、左、下、右)
    //注意:分割线的左侧的偏移量用这个方法不能设置
    _tableView.separatorInset = UIEdgeInsetsMake(0, -100, 0, 10);
《2》 头视图------------------------tableHeaderView
            尾视图------------------------tableFooterView
    //注意:头视图和尾视图的宽度手动设置无效,他会自动与屏幕齐宽
《3》单元格的行高 ------------------rowHeight --默认44  
         如果需要动态地确定行高需要在代理方法中实现
《4》section头视图行高-------------sectionHeaderHeight
         section尾视图行高-------------sectionFooterHeight
《5》表示图背景---------------------backgroundView
        他有时会影响你的背景设置,此时将其设为nil;

【3】方法(tableView的方法)
《1》刷新数据------------------------- reloadData
《2》根据indexPath,返回cell实例
         ------------------------------cellForRowAtIndexPath
《3》根据显示的cell,返回cell实例的数组
         -------------------------------visibleCells
《4》根据显示的cell,返回NSIndexPath实例的数组
          ------------------------------indexPathsForVisibleRows
《5》根据组、行的下标,构建其IndexPath
          NSIndexPath *indexpath = [NSIndexPath indexPathForRow:2 inSection:0];
《6》使数据显示到指定位置,可以配置动画(需先构建IndexPath)
          [self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition: animated:YES];
《7》刷新组数据:---reloadSections: withRowAnimation:
          NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
   
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];


//自适应高度
cell.textLabel.numberOfLines = 0;



总结三:tableView常用数据源委托方法(dataSource)<凡与数据相关的>

@required
// 返回多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 单元格对象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@optional
// 返回多少组(默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              
//组的头视图标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;   
// 组的尾视图标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;


总结四:tableView常用代理委托方法(delegate)<凡与UI相关的>

@optional

// Variable height support
//单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
//头视图行高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
//尾视图行高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// 自定义头/尾视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;  
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; 

// 当单元格被选中时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;


//当单元格即将出现时调用的方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
//当头视图即将出现时调用的方法
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
//当尾视图即将出现时调用的方法
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
//当单元格即将消失时调用的方法
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
//当头视图即将消失时调用的方法
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
//当尾视图即将消失时调用的方法
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值