IOS学习之路五(代码实现UITableView)
先展示一下运行结果:
代码实现:
1.先创建一个空项目:
2.创建一个Controller:(TableViewController)
在AppDelegate.h中声明属性:
- // AppDelegate.h
- // UITableViewDemo
- //
- // Created by WildCat on 13-8-6.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @class TableViewController;
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- @property (nonatomic,strong) TableViewController *tableViewController;
- @property (strong, nonatomic) UIWindow *window;
- @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
- @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- - (void)saveContext;
- - (NSURL *)applicationDocumentsDirectory;
- @end
在.m文件中实现:关键代码:
- #import "AppDelegate.h"
- #import "TableViewController.h"
- @implementation AppDelegate
- @synthesize tableViewController=_tableViewController;
- @synthesize managedObjectContext = _managedObjectContext;
- @synthesize managedObjectModel = _managedObjectModel;
- @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- self.tableViewController=[[TableViewController alloc] initWithNibName:nil bundle:NULL];
- [self.window addSubview:self.tableViewController.view];
- return YES;
- }
在TableViewController中声明:
- #import <UIKit/UIKit.h>
- @interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
- @property (nonatomic, strong) UITableView *myTableView;
- @end
在其.m文件中实现:
- //
- // TableViewController.m
- // UITableViewDemo
- //
- // Created by WildCat on 13-8-6.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import "TableViewController.h"
- @interface TableViewController ()
- @end
- @implementation TableViewController
- @synthesize myTableView;
- #pragma mark - 实现UITableViewDelegate的方法
- //当cell被点击时调用该方法
- - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- if ([tableView isEqual:self.myTableView]){
- NSLog(@"%@",
- [NSString stringWithFormat:@"Cell %ld in Section %ld is selected",
- (long)indexPath.row, (long)indexPath.section]); }
- }
- - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- CGFloat result = 20.0f;
- if ([tableView isEqual:self.myTableView]){
- result = 40.0f;
- }
- return result;
- }
- #pragma mark - 实现datasource的方法
- //确定section的个数
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- NSInteger result=0;
- if ([tableView isEqual:self.myTableView]) {
- result=3;
- }
- return result;
- }
- //每个section的行数
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- NSInteger result = 0;
- if ([tableView isEqual:self.myTableView]){
- switch (section){ case 0:{
- result = 3;
- break;
- } case 1:{
- result = 5; break;
- } case 2:{
- result = 8; break;
- } }
- }
- return result;
- }
- //设置每个静态的cell
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- UITableViewCell *result = nil;
- if ([tableView isEqual:self.myTableView]){
- static NSString *TableViewCellIdentifier = @"MyCells";
- result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
- if (result == nil){
- result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];
- }
- result.textLabel.text = [NSString stringWithFormat:@"Section %ld, Cell %ld", (long)indexPath.section,
- (long)indexPath.row];
- // result.backgroundColor=[UIColor greenColor]; //背景色
- result.detailTextLabel.text = @"详细内容";
- if (indexPath.section==0) {
- result.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //设置右边的样式
- }else if(indexPath.section==1){
- result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; //设置右边的样式
- }else{
- result.accessoryType = UITableViewCellAccessoryCheckmark; //设置右边的样式
- }
- /*
- 这两个附件的区别在于 disclosure indicator 不产生事件,而 detail disclosure 按钮在被点击时会向委托触发一个事件;也就是说,点击 cell 上的按键会 有不同效果。因此,detail disclosure 按钮允许用户在同一行上执行两个独立但相关的操 作。
- */
- // typedef enum {
- // UITableViewCellAccessoryNone, // don't show any accessory view
- // UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
- // UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
- // UITableViewCellAccessoryCheckmark // checkmark. doesn't track
- // } UITableViewCellAccessoryType;
- }
- return result;
- }
- //实现DetailDisclosure被点击出发的方法
- - (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
- /* Do something when the accessory button is tapped */
- NSLog(@"Accessory button is tapped for cell at index path = %@", indexPath);
- UITableViewCell *ownerCell = [tableView cellForRowAtIndexPath:indexPath];
- NSLog(@"Cell Title = %@", ownerCell.textLabel.text);
- }
- #pragma mark - TableViewController 的方法
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- self.myTableView =
- [[UITableView alloc] initWithFrame:self.view.bounds
- style:UITableViewStyleGrouped];//另一种样式:UITableViewStyleGrouped
- self.myTableView.dataSource = self;
- self.myTableView.delegate = self;
- self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
- [self.view addSubview:self.myTableView];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- self.myTableView = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
转载请注明:
新浪微博:http://weibo.com/u/3202802157
转载于:https://www.cnblogs.com/lixingle/p/3279115.html
IOS学习之路五(代码实现UITableView)相关推荐
- java 设置年轻代堆大小,[JVM学习之路]五堆(一)堆的内存结构参数设置分代思想内存分配...
[JVM学习之路]五堆(一)堆的内存结构参数设置分代思想内存分配 [JVM学习之路]五.堆(一)堆的内存结构.参数设置.分代思想.内存分配策略及TLAB 一.堆的核心概述 堆的特点: 1.一个jvm实 ...
- Android SurfaceFlinger 学习之路(五)----VSync 工作原理
原址 VSync信号的科普我们上一篇已经介绍过了,这篇我们要分析在SurfaceFlinger中的作用.(愈发觉得做笔记对自己记忆模块巩固有很多帮助,整理文章不一定是用来给别人看的,但一定是为加强自己 ...
- IOS学习之路--OC的基础知识
1.项目经验 2.基础问题 3.指南认识 4.解决思路 ios开发三大块: 1.Oc基础 2.CocoaTouch框架 3.Xcode使用 -------------------- CocoaTouc ...
- iOS学习之路十三(动态调整UITableViewCell的高度)
大概你第一眼看来,动态调整高度是一件不容易的事情,而且打算解决它的第一个想法往往是不正确的.在这篇文章中我将展示如何使图表单元格的高度能根据里面文本内容来动态改变,同时又不必子类化UITableVie ...
- IOS学习笔记(五)——UI基础UIWindow、UIView
在PC中,应用程序多是使用视窗的形式显示内容,手机应用也不例外,手机应用中要在屏幕上显示内容首先要创建一个窗口承载内容,iOS应用中使用UIWindow.UIView来实现内容显示. UIWindow ...
- Leaflet学习之路五——动态绘制图形(点、线、圆、多边形)
leaflet动态绘制图形 动态绘点 动态绘线 动态绘多边形 动态绘制矩形 2020.3.16更新 更新日志: 2019.1.14:更新了绘制多边形时tmpline没有移除的问题 2019.1.15: ...
- IOS学习之路七(使用 Operation 异步运行任务)
在 application delegate 头文件(.h)中声明一个 operation 队列和两个 invocation operations: #import <UIKit/UIKit.h ...
- Android学习之路五:Dialog和Toast
Dialog是一种长时间驻留的弹窗,只有在你想要它小时时才会消失, Toast是短时间弹窗,它会在显示消息后很快消失. Dialog案例一(只有"OK"): java代码: new ...
- Python学习之路五
文章目录 迭代器 高阶函数 map reduce sorted filter 推导式 列表推导式 集合推导式 字典推导式 生成器 迭代器 迭代器:能被next进行调用,并且不断返回一下值的对象. 特征 ...
最新文章
- 超高性能管线式HTTP请求(实践·原理·实现)
- 麟龙指标通达信指标公式源码_通达信指标公式源码神龙指标公式
- boost::pfr::tuple_size相关的测试程序
- 制作centos6的启动光盘boot.iso
- 在 SAP Gateway Demo System ES5 申请用户遇到问题该怎么处理
- 理论 | 教你彻底学会Java序列化和反序列化
- 阶段3 2.Spring_07.银行转账案例_1 今日课程内容介绍
- 手机上怎么去掉a 标签中的img点击时的阴影?
- 用java读取txt文件内容_java读取txt文件内容
- editormd文件上传
- pytorch动态调整学习率之Poly策略
- Day621.Spring Test 常见错误 -Spring编程常见错误
- python将多个列表合并_Python中多个列表与字典的合并方法
- linux下使用P4(命令行)
- 联想电脑计算机无法正常启动怎么办,电脑蓝屏无法启动怎么办
- 博弈论——序论(读书笔记)
- MODBUS通讯协议解析及实例
- 计算火车运行时间 本题要求根据火车的出发时间和达到时间,编写程序计算整个旅途所用的时间
- 12个超炫数据可视化工具
- Linux+开发+运维-推荐书籍与学习路线
热门文章
- java核心编程视频教学
- 【深度学习】Dropout与学习率衰减
- 常见OJ评判结果对照表
- 中运量71路线路图_浦东临港的中运量呼之欲出:临港地区已经成为上海建设的热土...
- C语言求m中n个数字的组合
- 进阶学习(4.2) JVM 常用配置参数, GC 参数
- 如何优化网站结构才促使网站排名“节节高”?
- springboot调整请求头大小_【SpringBoot WebFlux 系列】 header 参数解析
- 计算机电缆外径相差太大,DJYPVP计算机电缆标准外径
- Google TPU 揭密——看TPU的架构框图,矩阵加乘、Pool等处理模块,CISC指令集,必然需要编译器...