股票场内基金交易,没时间盯盘?
什么是 UITableView
在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView 继承自 UIScrollView,因此支持垂直滚动,而且性能极佳,使用简单方便而且功能强大。利用它可以实现:
UITableView 的两种基本样式
UITableViewStylePlain、UITableViewStyleGrouped
设置UITableView的 dataSource 数据源
UITableView 需要一个数据源 (dataSource) 来显示数据,它会向数据源查询一共有多少 section 多少行(row)数据以及每一行显示什么数据等,没有设置数据源的UITableView只是个空壳,无法显示内容。设置dataSource 数据源需要遵守 UITableViewDataSource 协议,凡是遵守 UITableViewDataSource 协议的 OC 对象,都可以是 UITableView 的数据源。
-
声明遵守 UITableViewDataSource 协议:
1@property (nonatomic, assign) id dataSource; -
在 implementation
1tableView.dataSource = dataSource -
必须实现的方法
12345678//调用数据源的下面方法得知一共有多少组数据- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;//调用数据源的下面方法得知每一组有多少行数据- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;//调用数据源的下面方法得知每一行显示什么内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;其中 indexPath.section 表示第几组,indexPath.row 表示第几行
UITableView 设置不同 section 的顶部和底部标题
1 |
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; |
还有
1 |
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; |
例如这个例子,需要自定义一个 @@@@@@@@
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#pragma mark - 懒加载 carGroups - (NSArray *)carGroups{ if (!_carGroups) { //1.创建模型 CarGroupData *carGroup1 = [[CarGroupData alloc] init]; carGroup1.title = @"德系品牌"; carGroup1.desc = @"坚不可摧,可以的"; carGroup1.cars = @[@"奥迪", @"宝马"]; CarGroupData *carGroup2 = [[CarGroupData alloc] init]; carGroup2.title = @"日韩品牌"; carGroup2.desc = @"还可以吧,不喜欢日货"; carGroup2.cars = @[@"本田",@"丰田",@"现代"]; CarGroupData *carGroup3 = [[CarGroupData alloc] init]; carGroup3.title = @"美国品牌"; carGroup3.desc = @"美国大兵哒哒哒哒"; carGroup3.cars = @[@"悍马",@"福特",@"大众",@"特斯拉"]; //但是 底部描述和顶部标题 是连在一起的,最好不搞底部描述 //2.将模型添加到数组中 _carGroups = @[carGroup3, carGroup1,carGroup2]; } return _carGroups; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { NSLog(@"self.carGroups.count = %d",self.carGroups.count); return self.carGroups.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //不可以这样 return self.carGroups[section].cars.count; //而是 CarGroupData *carGroup = self.carGroups[section]; return carGroup.cars.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //1. 创建 cell UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; //2.1 取出 carGroup CarGroupData *carGroup = self.carGroups[indexPath.section]; //2.2 设置数据 NSString *title = carGroup.cars[indexPath.row]; cell.textLabel.text = title; return cell; } - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ CarGroupData *carGroup = self.carGroups[section]; return carGroup.desc; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ CarGroupData *carGroup = self.carGroups[section]; return carGroup.title; } |
UITableView 顶部、底部视图
@property (nonatomic, strong, nullable) UIView *tableHeaderView;
// accessory view for above row content. default is nil. not to be confused with section header
@property (nonatomic, strong, nullable) UIView *tableFooterView;
// accessory view below content. default is nil. not to be confused with section footer
例如这个例子:在底部视图添加一个 UIButton
1 2 3 4 5 6 7 8 9 10 11 12 |
//不用设置默认宽度,设置了也是白设置,宽度默认和 tableView 等宽,但是要设置默认高度,否则为 0 ,不显示。要设置宽度只能建立 UIView,把 UIButton 放在 UIView 里, UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundColor:[UIColor orangeColor]]; //[button setBackgroundImage:[UIImage imageNamed:@"buttondelete"] forState:UIControlStateHighlighted]; tableView.tableFooterView = button; [button setTitle:@"查看全部" forState:UIControlStateNormal]; button.frame = CGRectMake(0, 0, 0, 44); [self.view addSubview:tableView]; self.tableView = tableView; tableView.contentInset = UIEdgeInsetsMake(0, 0, 44,0); |
UITableView 其他常用属性和方法
cell 被选中时实现的方法
1 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
设置分割线样式
仅用于 grouped style 类型
1 2 3 4 5 6 7 |
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched; typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) { UITableViewCellSeparatorStyleNone, UITableViewCellSeparatorStyleSingleLine, UITableViewCellSeparatorStyleSingleLineEtched // This separator style is only supported for grouped style table views currently } __TVOS_PROHIBITED; |
- 关闭状态栏显示
1 2 3 |
- (BOOL)prefersStatusBarHidden{ return YES; } |
UITableViewCell 介绍
UITableView 的每一行都是一个 UITableViewCell,通过 dataSource 的 tableView:cellForRowAtIndexPath: 方法来初始化每一行.
UITableViewCell 的 contentView
UITableViewCell 内部有个默认的子视图 :contentView,contentView是 UITableViewCell 所显示内容的父视图,可显示一些辅助指示视图
contentView下默认有3个子视图其中2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问)第3个是子视图UIImageView(通过UITableViewCell的imageView属性访问)UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用contentView的哪些子视图,以及这些在contentView中的位置
- 我们在自定义 cell 时,也应该把新创建的 UI 控件加入到 contentView 的子视图。
UITableViewCell的accessoryType
辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType 来显示,默认是UITableViewCellAccessoryNone (不显示辅助指示视图),其他值还包括了:
-
UITableViewCellAccessoryDisclosureIndicator
-
UITableViewCellAccessoryDetailDisclosureButton
-
UITableViewCellAccessoryCheckmark
还可以通过 cell 的 accessoryView 属性来自定义辅助指示视图(比如往右边放一个开关),就可以设置一个 UISwitch 然后让 ccessoryView 指向它。
设置 cell 的高度
-
更改 tableView 的 rowHeight 属性,但更改的是所有行的统一高度,例如:
1self.tableView.rowHeight = 60; -
要单独设置某个 cell 的高度则需要:
使用代理,添加 UITableViewDelegate, 令 self.tableView.delegate = self; 然后实现方法
1- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
设置 cell 的背景
cell 的背景有两个 view 属性,为 backgroundView 和 selectedBackgroundView,分别表示正常状态和 cell 被选中的状态。不建议设置 backgroundColor 属性,这样的话当 cell 被选中背景颜色就会失效,而且 backgroundView的backgroundColor 优先级高于 backgroundColor。
UITableView的性能优化(cell的循环利用)
iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象。
重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象。
还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell。
解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
使用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.定义一个cell的标识 static NSString *ID = @”njcell"; // 2.从缓存池中取出cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 3.如果缓存池中没有cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } // 4.设置cell的属性... return cell; } |
自定义Cell
iOS 自定义 cell 有两种方式
-
纯代码 ,用于 cell 直接很多不同的,如微博,子控件个数,位置不一样。
-
xib 用于有大量相同内容的,如游戏列表,团购列表
对于大部分 app 来说其 cell 需要实现特定功能都需要自定义,下星期再写具体自定义的方法和例子吧。
想获得去掉 5 元限制的证券账户吗?

如果您想去掉最低交易佣金 5 元限制,使用微信扫描左边小程序二维码,访问微信小程序「优财助手」,点击底部菜单「福利」,阅读文章「通过优财开证券账户无最低交易佣金 5 元限制」,按照文章步骤操作即可获得免 5 元证券账户,股票基金交易手续费率万 2.5。
请注意,一定要按照文章描述严格操作,如错误开户是无法获得免 5 元证券账户的。