iOS开发-UITableView常用方法

简介:

UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewDataSource,UITableViewDelegate然后根据需要是实现对应的方法即可。 UITableView有两个默认的内置风格,Plain和Grouped,Plain表明表格视图自身没有真正地在你自己实际地提供任何外观之前提供很多的外观,大部分情况下,它会做的唯一的事情是它会给你这些header和footer。Grouped表格视图是UIKit提供的分组风格。风格的话如果有特别的需求,还可以自定义分组的风格。

页面布局

页面比较简单,一个简单UITableView:

 

头文件中的不需要声明,需要实现一下协议:

1
2
3
@interface  ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
 
@end

Demo实现

声明三个数据用来展示数据: 

1
2
3
4
5
6
7
@interface  ViewController ()
{
     NSArray  *channelArr;
     NSMutableArray  *filmArr;
     NSMutableArray  *tvArr;
}
@end

 初始化数据:

1
2
3
4
5
6
7
- ( void )viewDidLoad {
     [ super  viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     channelArr=[[ NSArray  alloc] initWithObjects:@ "电影" ,@ "电视剧" , nil ];
     filmArr=[[ NSMutableArray  alloc] initWithObjects:@ "智取威虎山" ,@ "一步之遥" ,@ "匆匆那年" ,@ "北京爱情故事" , nil ];
     tvArr=[[ NSMutableArray  alloc] initWithObjects:@ "何以笙箫默" ,@ "锋刃" ,@ "陆小凤与花满楼" ,@ "武媚娘传奇" , nil ];
}

 设置分组的组数:

1
2
3
4
- ( NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
     NSLog (@ "%lu" ,(unsigned  long )channelArr.count);
     return  [channelArr count];
}

 设置分组的标题:

1
2
3
- ( NSString  *)tableView:(UITableView *)tableView titleForHeaderInSection:( NSInteger )section{
     return  [channelArr objectAtIndex:section];
}

设置每个分组下内容的个数:

1
2
3
4
5
6
7
8
9
10
11
12
- ( NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:( NSInteger )section{
     NSInteger  count=0;
     switch  (section) {
         case  0:
             count=[filmArr count];
             break ;
         case  1:
             count=[tvArr count];
             break ;
     }
     return  count;
}

设置每个分组下的具体内容:

1
2
3
4
5
6
7
8
9
10
11
12
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath  *)indexPath{
     UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil ];
     switch  (indexPath.section) {
         case  0:
             [cell.textLabel setText:[filmArr objectAtIndex:indexPath.row]];
             break ;
         case  1:
             [cell.textLabel setText:[tvArr objectAtIndex:indexPath.row]];
             break ;
     }
     return  cell;
}

 设置分组标题和底部的高度:

1
2
3
4
5
6
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:( NSInteger )section{
     return  40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:( NSInteger )section{
     return  0;
}

 设置点击事件:

1
2
3
4
5
6
7
8
9
10
11
12
13
- ( void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath  *)indexPath{
     NSString  *content;
     switch  (indexPath.section) {
         case  0:
             content=[ NSString  stringWithFormat:@ "%@-%@" ,channelArr[0],[filmArr objectAtIndex:indexPath.row]];
             break ;
         case  1:
                content=[ NSString  stringWithFormat:@ "%@-%@" ,channelArr[1],[tvArr objectAtIndex:indexPath.row]];
             break ;
     }
     UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:@ "当前位置:"  message:content delegate: self  cancelButtonTitle:@ "确定"  otherButtonTitles: nil ];
     [alterView show];
}

 最终效果:

 源代码:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
//  ViewController.m
//  TableView
//http://www.cnblogs.com/xiaofeixiang/
//  Created by keso on 15/1/24.
//  Copyright (c) 2015年 keso. All rights reserved.
//
 
#import "ViewController.h"
 
@interface  ViewController ()
{
     NSArray  *channelArr;
     NSMutableArray  *filmArr;
     NSMutableArray  *tvArr;
}
@end
 
@implementation  ViewController
 
- ( void )viewDidLoad {
     [ super  viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     channelArr=[[ NSArray  alloc] initWithObjects:@ "电影" ,@ "电视剧" , nil ];
     filmArr=[[ NSMutableArray  alloc] initWithObjects:@ "智取威虎山" ,@ "一步之遥" ,@ "匆匆那年" ,@ "北京爱情故事" , nil ];
     tvArr=[[ NSMutableArray  alloc] initWithObjects:@ "何以笙箫默" ,@ "锋刃" ,@ "陆小凤与花满楼" ,@ "武媚娘传奇" , nil ];
}
//设置分组的组数
- ( NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
     NSLog (@ "%lu" ,(unsigned  long )channelArr.count);
     return  [channelArr count];
}
//设置分组的标题
- ( NSString  *)tableView:(UITableView *)tableView titleForHeaderInSection:( NSInteger )section{
     return  [channelArr objectAtIndex:section];
}
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//    return @"我是底部";
//}
//设置每个分组的个数
- ( NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:( NSInteger )section{
     NSInteger  count=0;
     switch  (section) {
         case  0:
             count=[filmArr count];
             break ;
         case  1:
             count=[tvArr count];
             break ;
     }
     return  count;
}
//设置分组中具体的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath  *)indexPath{
     UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil ];
     switch  (indexPath.section) {
         case  0:
             [cell.textLabel setText:[filmArr objectAtIndex:indexPath.row]];
             break ;
         case  1:
             [cell.textLabel setText:[tvArr objectAtIndex:indexPath.row]];
             break ;
     }
     return  cell;
}
 
//分组标题的行高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:( NSInteger )section{
     return  40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:( NSInteger )section{
     return  0;
}
//选中点击事件
- ( void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath  *)indexPath{
     NSString  *content;
     switch  (indexPath.section) {
         case  0:
             content=[ NSString  stringWithFormat:@ "%@-%@" ,channelArr[0],[filmArr objectAtIndex:indexPath.row]];
             break ;
         case  1:
                content=[ NSString  stringWithFormat:@ "%@-%@" ,channelArr[1],[tvArr objectAtIndex:indexPath.row]];
             break ;
     }
     UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:@ "当前位置:"  message:content delegate: self  cancelButtonTitle:@ "确定"  otherButtonTitles: nil ];
     [alterView show];
}
- ( void )didReceiveMemoryWarning {
     [ super  didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
}
 
@end
本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4246563.html,如需转载请自行联系原作者
相关文章
|
27天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
83 3
|
27天前
|
移动开发 前端开发 数据安全/隐私保护
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
26 0
|
3月前
|
存储 监控 iOS开发
iOS应用崩溃了,如何通过崩溃手机连接电脑查找日志方法
在iOS应用开发过程中,调试日志和奔溃日志是开发者必不可少的工具。当iOS手机崩溃时,我们可以连接电脑并使用Xcode Console等工具来查看日志。然而,这种方式可能不够方便,并且处理奔溃日志也相当繁琐。克魔助手的出现为开发者带来了极大的便利,本文将详细介绍其功能和使用方法。 克魔助手会提供两种日志,一种是实时的,一种的是崩溃的。(由于崩溃日志的环境很麻烦,目前只展示实时日志操作步骤)
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
38 0
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
89 2
|
27天前
|
Android开发 iOS开发 开发者
App备案-iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹的获取方法
App备案-iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹的获取方法
79 0
|
1天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
8 0
|
16天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
27天前
|
安全 编译器 开发工具
​iOS安全加固方法及实现
​iOS安全加固方法及实现
20 0