iOS开发-UITableView常用方法
UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewDataSource,UITableViewDelegate然后根据需要是实现对应的方法即可。 UITableView有两个默认的内置风格,Plain和Grouped,Plain表明表格视图自身没有真正地在你自己实际地提供任何外观之前提供很多的外观,大部分情况下,它会做的唯一的事情是它会给你这些header和footer。Grouped表格视图是UIKit提供的分组风格。风格的话如果有特别的需求,还可以自定义分组的风格。
页面布局
页面比较简单,一个简单UITableView:
头文件中的不需要声明,需要实现一下协议:
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> @end
Demo实现
声明三个数据用来展示数据:
@interface ViewController ()
{
NSArray *channelArr;
NSMutableArray *filmArr;
NSMutableArray *tvArr;
}
@end
初始化数据:
- (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];
}
设置每个分组下内容的个数:
- (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];
}
最终效果:
源代码:
//
// 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
iOS开发-UITableView常用方法的更多相关文章
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发,UITableView相关问题
第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...
- iOS开发-UITableView自定义Cell
UITableView在iOS中开发的重要地位是毋庸置疑的,基本上应用中用到的比例是一半左右,而且大部分情况都是需要自定义单元格的,这样用户看到的App才能更有美感.之前写过UITableView的基 ...
- iOS开发 UITableView之cell
1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...
- iOS开发-UITableView表格优化
之前的一篇文章大概讲述了一下UITableView的使用,UITableView在iOS的地位和ListView在Android中的地位基本上算是不相上下,关于ListView的优化网上的也有很多文章 ...
- iOS开发UITableView的动画cell
1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...
- iOS开发-UITableView滑动视差
视差滚动是指让多层背景以不同的速度移动,形成立体的运动效果,在Web上应用的比较多,App中倒是见的相对比较少,主要在UITableView中的应用的比较多,尤其是当整个UITableViewCell ...
- iOS开发-UITableView顶部图片下拉放大
关于顶部图片下拉放大,在用户展示的个人中心显示用户个人头像信息,设置UITableView的headerView实现,UITableView继承自UIScrollView,同样的设置UIScrollV ...
随机推荐
- CI框架的事务开启、提交和回滚
1.运行事务 $this->db->trans_start(); // 开启事务$this->db->query('一条SQL查询...');$this->db-> ...
- IOS和Android音频开发总结
最近因为项目需要对声音进行变声,所以边学习边做,发现音频的处理思路并不难,但是做起来还是有些繁琐的(比预期的) 趁着脑子还发热,赶紧把思路总结一下,记录下来. 主要讲三个部分 1,如何变声2,安卓实现 ...
- maven 发布jar包到远程仓库
有的时候我们需要发布一些自己写的相关jar包到maven私服,供项目组使用. 首先在setting.xml文件添加,这里 注意 要保证该账户有发布的权限 <servers> <ser ...
- android无后缀二进制执行文件替代apk实现程序功能
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha android无后缀二进制执行文件替代apk实现程序功能 实现将data/Android ...
- NOIP2018 生气记
今年的题都不怎么难 只是考到的东西相当相当的奇怪... 不想写题解,写出来感觉只是伤心的事 .................... Day1 一进考场就感受到了比去年要严一些... 花了1小时30分 ...
- 【枚举】【贪心】Codeforces Round #482 (Div. 2) B. Treasure Hunt
题意:给你3个字符串,3个人各对自己的字符串执行n轮操作,每一次选择一个字符变为任意一个和原来不同的字符.最后问你谁能使自己的串中的任意重复子串出现的次数最大化. 显然只需关注字符而非子串. 枚举每个 ...
- 在线HTTP速度测试(响应时间测试)及浏览器兼容测试
一.前言 网站的响应时间,是判断一个网站是否是好网站的重要的因素之一.百度首页的响应时间在全国各个省份小于10ms.这个响应时间远远好于竞争对手.根据美丽说的技术负责人分析,美丽说访问速度提升10%, ...
- 【转 记录】python中的encode以及decode
字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...
- 数据表-java类的映射
1.一个数据表对应一个java类 2.数据表的字段对应java类的属性 3.一对多的数据表关系 一方用一个java对象表示 多方用一个java对象数组表示 4.多对多的数据表关系:采用中间表,将多对多 ...
- String对象池的作用
我们知道得到String对象有两种办法:String str1="hello";String str2=new String("hello"); 这两种 ...