iOS开发技能iOS基础知识总结(持续更新...)iOS学习

UITableView基础

2016-11-09  本文已影响240人  非典型技术宅

1. UITableView的作用

2. UITableView的常用属性

2.1 分割线属性

属性名称 作用
separatorStyle 分隔线样式
separatorColor 分隔线颜色

2.2 选中属性

属性名称 作用
allowsSelection 允许选中
allowsMultipleSelection 允许多选

2.3 行数

属性名称 作用
indexPathsForSelectedRows 当前选中行数
indexPathsForVisibleRows 当前可见行数

2.4 背景

属性名称 作用
backgroundView 背景视图
selectedBackgroundView 选中时的背景视图

2.5 UITableViewCell的selectionStyle属性可设置被选中时的背景颜色

属性名称 作用
UITableViewCellSelectionStyleNone 没有颜色
UITableViewCellSelectionStyleBlue 蓝色(默认)
UITableViewCellSelectionStyleGray 灰色

3. tableView展示数据三部曲

  1. 遵守数据源协议;
  2. 设置数据源
  3. 实现相应数据源方法

3.1 遵守数据源

@interface ViewController ()<UITableViewDataSource>

3.2 设置数据源

    self.tableView.dataSource = self;

3.3 实现数据源方法

  1. 总共多少组
  2. 每组多少行
  3. 每组中每行的内容
//返回有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

//返回有多少行,section 组的索引
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

//返回每一组的每一行显示什么内容
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //创建UITableViewCell
    UITableViewCell *tableViewCell = [[UITableViewCell alloc]init];
    
    return tableViewCell;
}

4. UITableViewStyleGrouped样式

4.1 使用storyboard设置

Paste_Image.png

4.2 使用代码设置

Paste_Image.png
    UITableView *haha = [UITableView alloc]initWithFrame:<#(CGRect)#> style:(UITableViewStyle)];

5. UITableViewCell

5.1 四种默认样式

Paste_Image.png
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
    UITableViewCellStyleValue1,     // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
    UITableViewCellStyleValue2,     // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
    UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
};             // available in iPhone OS 3.0

满足不了需求,就需要自定义样式。

上一篇 下一篇

猜你喜欢

热点阅读