先展示一下运行结果:

代码实现:

1.先创建一个空项目:

2.创建一个Controller:(TableViewController)

在AppDelegate.h中声明属性:

[cpp] view plaincopyprint?
  1. //  AppDelegate.h
  2. //  UITableViewDemo
  3. //
  4. //  Created by WildCat on 13-8-6.
  5. //  Copyright (c) 2013年 wildcat. All rights reserved.
  6. //
  7. #import <UIKit/UIKit.h>
  8. @class TableViewController;
  9. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  10. @property (nonatomic,strong) TableViewController *tableViewController;
  11. @property (strong, nonatomic) UIWindow *window;
  12. @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
  13. @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
  14. @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  15. - (void)saveContext;
  16. - (NSURL *)applicationDocumentsDirectory;
  17. @end

在.m文件中实现:关键代码:

[cpp] view plaincopyprint?
  1. #import "AppDelegate.h"
  2. #import "TableViewController.h"
  3. @implementation AppDelegate
  4. @synthesize tableViewController=_tableViewController;
  5. @synthesize managedObjectContext = _managedObjectContext;
  6. @synthesize managedObjectModel = _managedObjectModel;
  7. @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
  8. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  9. {
  10. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  11. // Override point for customization after application launch.
  12. self.window.backgroundColor = [UIColor whiteColor];
  13. [self.window makeKeyAndVisible];
  14. self.tableViewController=[[TableViewController alloc] initWithNibName:nil bundle:NULL];
  15. [self.window addSubview:self.tableViewController.view];
  16. return YES;
  17. }

在TableViewController中声明:

[cpp] view plaincopyprint?
  1. #import <UIKit/UIKit.h>
  2. @interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
  3. @property (nonatomic, strong) UITableView *myTableView;
  4. @end

在其.m文件中实现:

[cpp] view plaincopyprint?
  1. //
  2. //  TableViewController.m
  3. //  UITableViewDemo
  4. //
  5. //  Created by WildCat on 13-8-6.
  6. //  Copyright (c) 2013年 wildcat. All rights reserved.
  7. //
  8. #import "TableViewController.h"
  9. @interface TableViewController ()
  10. @end
  11. @implementation TableViewController
  12. @synthesize myTableView;
  13. #pragma  mark - 实现UITableViewDelegate的方法
  14. //当cell被点击时调用该方法
  15. - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  16. if ([tableView isEqual:self.myTableView]){
  17. NSLog(@"%@",
  18. [NSString stringWithFormat:@"Cell %ld in Section %ld is selected",
  19. (long)indexPath.row, (long)indexPath.section]); }
  20. }
  21. - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  22. CGFloat result = 20.0f;
  23. if ([tableView isEqual:self.myTableView]){
  24. result = 40.0f;
  25. }
  26. return result;
  27. }
  28. #pragma mark - 实现datasource的方法
  29. //确定section的个数
  30. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  31. NSInteger result=0;
  32. if ([tableView isEqual:self.myTableView]) {
  33. result=3;
  34. }
  35. return result;
  36. }
  37. //每个section的行数
  38. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  39. NSInteger result = 0;
  40. if ([tableView isEqual:self.myTableView]){
  41. switch (section){ case 0:{
  42. result = 3;
  43. break;
  44. } case 1:{
  45. result = 5; break;
  46. } case 2:{
  47. result = 8; break;
  48. } }
  49. }
  50. return result;
  51. }
  52. //设置每个静态的cell
  53. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  54. UITableViewCell *result = nil;
  55. if ([tableView isEqual:self.myTableView]){
  56. static NSString *TableViewCellIdentifier = @"MyCells";
  57. result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
  58. if (result == nil){
  59. result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];
  60. }
  61. result.textLabel.text = [NSString stringWithFormat:@"Section %ld, Cell %ld", (long)indexPath.section,
  62. (long)indexPath.row];
  63. // result.backgroundColor=[UIColor greenColor];  //背景色
  64. result.detailTextLabel.text = @"详细内容";
  65. if (indexPath.section==0) {
  66. result.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  //设置右边的样式
  67. }else if(indexPath.section==1){
  68. result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;  //设置右边的样式
  69. }else{
  70. result.accessoryType = UITableViewCellAccessoryCheckmark;  //设置右边的样式
  71. }
  72. /*
  73. 这两个附件的区别在于 disclosure indicator 不产生事件,而 detail disclosure 按钮在被点击时会向委托触发一个事件;也就是说,点击 cell 上的按键会 有不同效果。因此,detail disclosure 按钮允许用户在同一行上执行两个独立但相关的操 作。
  74. */
  75. //        typedef enum {
  76. //            UITableViewCellAccessoryNone,                   // don't show any accessory view
  77. //            UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
  78. //            UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
  79. //            UITableViewCellAccessoryCheckmark               // checkmark. doesn't track
  80. //        } UITableViewCellAccessoryType;
  81. }
  82. return result;
  83. }
  84. //实现DetailDisclosure被点击出发的方法
  85. - (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
  86. /* Do something when the accessory button is tapped */
  87. NSLog(@"Accessory button is tapped for cell at index path = %@", indexPath);
  88. UITableViewCell *ownerCell = [tableView cellForRowAtIndexPath:indexPath];
  89. NSLog(@"Cell Title = %@", ownerCell.textLabel.text);
  90. }
  91. #pragma mark - TableViewController 的方法
  92. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  93. {
  94. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  95. if (self) {
  96. // Custom initialization
  97. }
  98. return self;
  99. }
  100. - (void)viewDidLoad
  101. {
  102. [super viewDidLoad];
  103. self.view.backgroundColor = [UIColor whiteColor];
  104. self.myTableView =
  105. [[UITableView alloc] initWithFrame:self.view.bounds
  106. style:UITableViewStyleGrouped];//另一种样式:UITableViewStyleGrouped
  107. self.myTableView.dataSource = self;
  108. self.myTableView.delegate = self;
  109. self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
  110. [self.view addSubview:self.myTableView];
  111. }
  112. - (void)viewDidUnload
  113. {
  114. [super viewDidUnload];
  115. // Release any retained subviews of the main view.
  116. self.myTableView = nil;
  117. }
  118. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  119. {
  120. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  121. }

转载请注明:

新浪微博:http://weibo.com/u/3202802157

转载于:https://www.cnblogs.com/lixingle/p/3279115.html

IOS学习之路五(代码实现UITableView)相关推荐

  1. java 设置年轻代堆大小,[JVM学习之路]五堆(一)堆的内存结构参数设置分代思想内存分配...

    [JVM学习之路]五堆(一)堆的内存结构参数设置分代思想内存分配 [JVM学习之路]五.堆(一)堆的内存结构.参数设置.分代思想.内存分配策略及TLAB 一.堆的核心概述 堆的特点: 1.一个jvm实 ...

  2. Android SurfaceFlinger 学习之路(五)----VSync 工作原理

    原址 VSync信号的科普我们上一篇已经介绍过了,这篇我们要分析在SurfaceFlinger中的作用.(愈发觉得做笔记对自己记忆模块巩固有很多帮助,整理文章不一定是用来给别人看的,但一定是为加强自己 ...

  3. IOS学习之路--OC的基础知识

    1.项目经验 2.基础问题 3.指南认识 4.解决思路 ios开发三大块: 1.Oc基础 2.CocoaTouch框架 3.Xcode使用 -------------------- CocoaTouc ...

  4. iOS学习之路十三(动态调整UITableViewCell的高度)

    大概你第一眼看来,动态调整高度是一件不容易的事情,而且打算解决它的第一个想法往往是不正确的.在这篇文章中我将展示如何使图表单元格的高度能根据里面文本内容来动态改变,同时又不必子类化UITableVie ...

  5. IOS学习笔记(五)——UI基础UIWindow、UIView

    在PC中,应用程序多是使用视窗的形式显示内容,手机应用也不例外,手机应用中要在屏幕上显示内容首先要创建一个窗口承载内容,iOS应用中使用UIWindow.UIView来实现内容显示. UIWindow ...

  6. Leaflet学习之路五——动态绘制图形(点、线、圆、多边形)

    leaflet动态绘制图形 动态绘点 动态绘线 动态绘多边形 动态绘制矩形 2020.3.16更新 更新日志: 2019.1.14:更新了绘制多边形时tmpline没有移除的问题 2019.1.15: ...

  7. IOS学习之路七(使用 Operation 异步运行任务)

    在 application delegate 头文件(.h)中声明一个 operation 队列和两个 invocation operations: #import <UIKit/UIKit.h ...

  8. Android学习之路五:Dialog和Toast

    Dialog是一种长时间驻留的弹窗,只有在你想要它小时时才会消失, Toast是短时间弹窗,它会在显示消息后很快消失. Dialog案例一(只有"OK"): java代码: new ...

  9. Python学习之路五

    文章目录 迭代器 高阶函数 map reduce sorted filter 推导式 列表推导式 集合推导式 字典推导式 生成器 迭代器 迭代器:能被next进行调用,并且不断返回一下值的对象. 特征 ...

最新文章

  1. 超高性能管线式HTTP请求(实践·原理·实现)
  2. 麟龙指标通达信指标公式源码_通达信指标公式源码神龙指标公式
  3. boost::pfr::tuple_size相关的测试程序
  4. 制作centos6的启动光盘boot.iso
  5. 在 SAP Gateway Demo System ES5 申请用户遇到问题该怎么处理
  6. 理论 | 教你彻底学会Java序列化和反序列化
  7. 阶段3 2.Spring_07.银行转账案例_1 今日课程内容介绍
  8. 手机上怎么去掉a 标签中的img点击时的阴影?
  9. 用java读取txt文件内容_java读取txt文件内容
  10. editormd文件上传
  11. pytorch动态调整学习率之Poly策略
  12. Day621.Spring Test 常见错误 -Spring编程常见错误
  13. python将多个列表合并_Python中多个列表与字典的合并方法
  14. linux下使用P4(命令行)
  15. 联想电脑计算机无法正常启动怎么办,电脑蓝屏无法启动怎么办
  16. 博弈论——序论(读书笔记)
  17. MODBUS通讯协议解析及实例
  18. 计算火车运行时间 本题要求根据火车的出发时间和达到时间,编写程序计算整个旅途所用的时间
  19. 12个超炫数据可视化工具
  20. Linux+开发+运维-推荐书籍与学习路线

热门文章

  1. java核心编程视频教学
  2. 【深度学习】Dropout与学习率衰减
  3. 常见OJ评判结果对照表
  4. 中运量71路线路图_浦东临港的中运量呼之欲出:临港地区已经成为上海建设的热土...
  5. C语言求m中n个数字的组合
  6. 进阶学习(4.2) JVM 常用配置参数, GC 参数
  7. 如何优化网站结构才促使网站排名“节节高”?
  8. springboot调整请求头大小_【SpringBoot WebFlux 系列】 header 参数解析
  9. 计算机电缆外径相差太大,DJYPVP计算机电缆标准外径
  10. Google TPU 揭密——看TPU的架构框图,矩阵加乘、Pool等处理模块,CISC指令集,必然需要编译器...