iOSiOS分享的demo首页投稿(暂停使用,暂停投稿)

iOS-基础控件--UITableView(1:基本知识)

2016-06-13  本文已影响280人  云之君兮鹏
**雨中黄叶树,灯下白头人!**<草苗龟>

  • UITableView继承于UIScrollView,可以滚动


代码展示:

// 创建

UITableView *tableview = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
[self.view addSubview:tableview];

// 每一行高

tableview.rowHeight = 50.0;

// 分割线颜色

tableview.separatorColor = [UIColor redColor];

// 分割线样式

tableview.separatorStyle = 1;

// 头部视图

UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
headView.backgroundColor = [UIColor yellowColor];
tableview.tableHeaderView = headView;

// 底部视图

UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
footView.backgroundColor = [UIColor greenColor];
tableview.tableFooterView = footView;

#pragma mark ----cell的重用机制

  1. 当 个cell 被滑出屏幕,这个ce 会被系统放到相应的重 池中。
    2.当 tableview 需要显 个cell ,会先去重 池中尝试获取 个cell。
    3.如果重 池没有ce ,就会创建 个cell 。
    4.取得ce 之后会重新赋值进 使 。

// 第二种方法 :
// 第一步 : 注册cell
// 参数1 : 是一个类(当重用池中没有cell的时候开始创建cell类)
// 参数2 : 给重用池一个重用标示符

[tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

#pragma mark ----设置代理(处理数据和UI界面)

// 用于处理显示视图的相关内容<UITableViewDelegate>

  tableview.delegate = self;

// 用于处理数据相关的内容<UITableViewDataSource>

tableview.dataSource = self;

// 设置每个区有多少行

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
 }

// 设置分区数

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 2;
}

// 设置每行cell的样式和数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
       /* // 第一种
// 第一步 : 现在重用池中去取重用的cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

// 第二步 : 如果重用池中没有,需要重新的创建一个
if (cell == nil)
{
 cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
}
*/

// 第二种方法  (一定要注册cell)
// 第二步 : 根据重用池标示符去取重用池的cell在这使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
// 设置文本
cell.textLabel.text = [NSString stringWithFormat:@"第 %ld 区 --- 第 %ld 行",indexPath.section,indexPath.row];
return cell;

}

// 设置头部标题

  - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 {
return [NSString stringWithFormat:@"这是 %ld 区头标题",section];
 }

// 设置尾部标题

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
 {
return [NSString stringWithFormat:@"这是 %ld 区尾标题",section];
}

// 设置右侧索引

  - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
 {
    return @[@"a",@"b"];
}  

// 设置每个区头部的高度

  - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
 {
return 150;
 }

// 设置每行的高度 (可根据下标AtIndexPath进行专门的设置)

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  if (indexPath.row == 0)
 {
    return 100;
}
return 50;

}

// 设置尾部的高度

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 200;
}
结果展示部分

数据和操作部分参考这个里面


上一篇下一篇

猜你喜欢

热点阅读