IOS开发学习笔记027-UITableView 使用模型对象

1、模型对象

继续优化上一个程序

上一次用到字典,但是坏处多多。这里将这些数据封装到类中。

这就是MVC中得模型,模型就是数据的显示结构

新建一个类,添加几个属性和一个类方法用于快速返回对象

 1 #import <Foundation/Foundation.h>
 2
 3 @interface Province : NSObject
 4 // UI控件用weak
 5 // nsstring 用copy
 6
 7
 8 @property (nonatomic,copy) NSString *header;
 9 @property (nonatomic,copy) NSString *footer;
10 @property (nonatomic,strong) NSArray *cities;
11 //
12 + (id)provinceWithHeader:(NSString *)header andFooter:(NSString *)footer andCities:(NSArray *)cities;
13 @end
 1 #import "Province.h"
 2
 3 @implementation Province
 4
 5
 6 + (id)provinceWithHeader:(NSString *)header andFooter:(NSString *)footer andCities:(NSArray *)cities
 7 {
 8     Province *pp = [[Province alloc] init];
 9     pp.footer = footer;
10     pp.header = header;
11     pp.cities = cities;
12     return pp;
13 }
14 @end

初始化对象时使用类方法

1     Province *gd = [Province provinceWithHeader:@"广东" andFooter:@"广东怒啊啊" andCities:@[@"广州",@"深圳"]];
2     Province *hn = [Province provinceWithHeader:@"湖南" andFooter:@"湖南哈哈哈啊啊" andCities:@[@"长沙",@"岳阳"]];

修改一下 方法 numberOfRowsInSection

1 // 2 设置每组多少行 row
2 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3 {
4     // 取出对象中得数据
5     return [_allProvince[section] cities].count;
6 }

修改一下方法 cellForRowAtIndexPath

 1 // 3 返回每一行显示的内容
 2 // indexPath 标识唯一的一行,第几组第几行
 3 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 4 {
 5     UITableViewCell *tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
 6     // 设置cell显示的文字
 7     NSString *text =  [_allProvince[indexPath.section] cities][indexPath.row];
 8     tableCell.textLabel.text = text;
 9     return tableCell;
10 }

修改一下方法 titleForHeaderInSection

1 #pragma mark 第section组的头部标题
2 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
3 {
4     return  [_allProvince[section] header];
5 }

在修改一下  titleForFooterInSection

1 #pragma mark 第section组的尾部显示
2 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
3 {
4     return [_allProvince[section] footer];
5 }

效果是一样的,但是代码的可扩展性更好了。

显示表格右侧的索引

需要用法一个方法

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

返回的数组就是要现实的索引数组,单击索引文字会跳转到对应的组

 1 #pragma mark 返回表格右边显示的索引条
 2 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
 3 {
 4     NSMutableArray *title = [NSMutableArray array];
 5     for (Province *p in _allProvince)
 6     {
 7         [title addObject:p.header]; // 获取标题显示在索引中
 8     }
 9     return title;
10 }

2、单组数据的显示

以上说到的都是多组数据的显示,下面说单组数据的显示。

主要是在创建view时指定style参数为Plain

设置组和行

 1 // 设置行,既然是单组,那就只有一行
 2 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 3 {
 4     return 1;
 5 }
 6 // 设置行
 7 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 8 {
 9     return 9;
10 }
11 // 设置行内容
12 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
13 {
14     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
15     cell.textLabel.text = [NSString stringWithFormat:@"第%d行数据",indexPath.row]; // 中间文字
16     cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"00%d.png",indexPath.row + 1]]; // 左侧图像
17     cell.detailTextLabel.text = [NSString stringWithFormat:@"第%d行数据的描述",indexPath.row]; //描述文字,对textLable的描述
18     return cell;
19 }

其中UITableViewCell的几种显示方式:

UITableViewCellStyleDefault:不显示子标题

UITableViewCellStyleValue1:detial标签显示在右边

UITableViewCellStyleValue2:不显示图片

UITableViewCellStyleSubTitle:显示子标题

设置显示在最右侧的按钮或者图标  

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 最右侧指示信息

UITableViewCellAccessoryCheckmark 最右侧显示一个对号

UITableViewCellAccessoryDetailButton 最右侧显示一个i按钮

UITableViewCellAccessoryDetailDisclosureButton 显示一个I按钮和一个尖括号>

UITableViewCellAccessoryDisclosureIndicator 显示一个尖括号 >

最终效果是这样

代码如下

 1 //
 2 //  SLQViewController.m
 3 //  UITableView-单组数据显示
 4 //
 5 //  Created by Christian on 15/5/16.
 6 //  Copyright (c) 2015年 slq. All rights reserved.
 7 //
 8
 9 #import "SLQViewController.h"
10
11 @interface SLQViewController () <UITableViewDataSource, UITableViewDelegate>
12
13 @end
14
15 @implementation SLQViewController
16
17 - (void)viewDidLoad
18 {
19     [super viewDidLoad];
20     // Do any additional setup after loading the view, typically from a nib.
21
22 }
23
24
25 // 设置行,既然是单组,那就只有一行
26 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
27 {
28     return 1;
29 }
30 // 设置行
31 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
32 {
33     return 9;
34 }
35 // 设置行内容
36 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
37 {
38     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
39     cell.textLabel.text = [NSString stringWithFormat:@"第%ld行数据",indexPath.row]; // 中间文字
40     cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"00%ld.png",indexPath.row + 1]]; // 左侧图像
41     cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld行数据的描述",indexPath.row]; // 描述信息
42     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 最右侧指示信息
43     return cell;
44 }
45
46
47 @end
时间: 2024-12-18 11:58:43

IOS开发学习笔记027-UITableView 使用模型对象的相关文章

IOS开发学习笔记-(2)键盘控制,键盘类型设置,alert 对话框

一.关闭键盘,放弃第一响应者,处理思路有两种 ① 使用文本框的 Did End on Exit 绑定事件 ② UIControl on Touch 事件 都去操作 sender 的  resignFirstResponder #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *txtUserName; @pro

iOS开发学习笔记:基础篇

iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境),Xcode是一个集成开发环境,包括了编辑器.调试.模拟器等等一系列方便开发和部署的工具,iOS SDK则是开发应用所必需,不同的SDK分别对应不同的iOS版本或设备,通常我们需要下载多个iOS SDK以确保我们开发的程序能够在不同版本的iOS上正常运行. 创建新工程 Xcode提供了很多种工程模

IOS开发学习笔记-(3) 进度条、等待动画开始停止

一.创建对应空间视图  ,如下图: 二.编写对应的 .h 代码,如下 : #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activWaitNetWork; @property (weak, nonatomic) IBOutlet UIProgressView *pgrsDownLo

IOS开发学习笔记(二)-语音识别(科大讯飞)

上次简单地讲解了如何利用科大讯飞完成语音合成,今天接着也把语音识别整理一下.当然,写代码前我们需要做的一些工作(如申请appid.导库),在上一篇语音合成的文章当中已经说过了,不了解的可以看看我上次的博文,那么这次直接从堆代码开始吧. 详细步骤: 1.导完类库之后,在工程里添加好用的头文件.在视图里只用了一个UITextField显示识别的内容,两个UIButton(一个开始监听语音,一个结束监听),然后引入类.添加代理,和语音合成的一样. MainViewController.h 1 #imp

IOS开发学习笔记(1)-----UILabel 详解

1. [代码][C/C++]代码     //创建uilabelUILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];//设置背景色label1.backgroundColor = [UIColor grayColor];//设置taglabel1.tag = 91;//设置标签文本label1.text = @"Hello world!";//设置标签文本字体和字体大小label1.

IOS开发学习笔记(2)-----UIButton 详解

1. [代码][C/C++]代码     //这里创建一个圆角矩形的按钮    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    //    能够定义的button类型有以下6种,//    typedef enum {//        UIButtonTypeCustom = 0,          自定义风格//        UIButtonTypeRoundedRect,        

IOS开发学习笔记--语音合成(科大讯飞)

      现在语音服务越来越热,我们平时使用的很多软件都带有语音合成和识别功能,用起来也很方便.说到语音服务,Google和微软都提供过API接口,不过笔者要介绍的是国内的智能语音技术提供商---科大讯飞.之前看过一个比较Google.微软和科大讯飞语音识别引擎的博文(http://fqctyj.blog.163.com/blog/static/70843455201361955322797/),有兴趣可以去看看.笔者接触语音服务的时间也不长,对语音服务也不是很了解,但是拆解过科大讯飞的Dem

IOS开发学习笔记017-什么是IOS开发

应用程序开发流程 1.IOS开发需要思考的问题 用户是谁?不同应用程序的内容和用户体验大不相同,这取决于想要编写的是什么应用程序,它可能是儿童游戏,也可能是待办事项列表应用程序,又或者是测试自己学习成果的应用程序. 应用程序的用途是什么?赋予应用程序一个明确的用途十分重要.了解激发用户使用应用程序的动因是界定用途的一个出发点. 应用程序尝试解决什么问题?应用程序应该完美解决单个问题,而不是尝试解决多个截然不同的问题.如果发现应用程序尝试解决不相关的问题,那么最好考虑编写多个应用程序. 应用程序要

ios开发学习笔记(1)

objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = [UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];//1.从Infor.plist中取出版本号NString *version = [NSBundle mainBundle].infoDictionary[key];//2.