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

来源:互联网 发布:演讲书籍推荐知乎 编辑:程序博客网 时间:2024/06/05 21:07
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 = [tableViewdequeueReusableCellWithIdentifier: forIndexPath: ];
(4.2.1)(xib)中,选中cell, 设置identifier
(4.2.2)在.m文件中 viewDidLoad方法中,在注册一个单元格(UI07-06)
( //注册一个单元格(通过类去注册)---(不建议)
    [tableView registerClass:[UITableViewCellclass]forCellReuseIdentifier:@"Cell"];)
(5)如果从别的ViewController弹出本视图控制器的话,弹出时需用到以下方法:

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




总结二:tableView属性和方法
【1】创建
    UITableView *tableView = [[UITableView alloc] initWithFrame:  style: ];
   //frame设置方法一:
   CGRectMake(0,20, [UIScreenmainScreen].bounds.size.width, [UIScreenmainScreen].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 = [NSIndexPathindexPathForRow:2inSection:0];
《6》使数据显示到指定位置,可以配置动画(需先构建IndexPath)
          [self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition: animated:YES];
《7》刷新组数据:---reloadSections:withRowAnimation:
          NSIndexSet *indexSet = [NSIndexSetindexSetWithIndex:section];
   
    [self.tableViewreloadSections:indexSetwithRowAnimation: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)sectionNS_AVAILABLE_IOS(6_0);
//当尾视图即将出现时调用的方法
- (void)tableView:(UITableView*)tableView willDisplayFooterView:(UIView*)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);
//当单元格即将消失时调用的方法
- (void)tableView:(UITableView*)tableView didEndDisplayingCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPathNS_AVAILABLE_IOS(6_0);
//当头视图即将消失时调用的方法
- (void)tableView:(UITableView*)tableView didEndDisplayingHeaderView:(UIView*)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);
//当尾视图即将消失时调用的方法
- (void)tableView:(UITableView*)tableView didEndDisplayingFooterView:(UIView*)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);


0 0