iOS开发UI篇—直接使用UITableView Controller

iOS开发UI篇—直接使用UITableView Controller

一、一般过程

复制代码
 1 //
 2 //  YYViewController.h
 3 //  UITableView Controller
 4 //
 5 //  Created by 孔医己 on 14-6-2.
 6 //  Copyright (c) 2014年 itcast. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface YYViewController : UIViewController
12 
13 @end
复制代码

系统storyboard中默认的控制器为:ViewController

这样的话如果整个程序界面都只是使用UITableView来搭建,那么一般需要完成以下相对繁琐的步骤:

(1)向界面上拖一个UItableview

(2)设置数据源

(3)设置代理

(4)遵守代理协议

 上述过程相对繁琐,且还需要手动的设置数据源,代理,遵守协议等,容易遗漏,下面推荐直接使用UITableView Controller。
 
二、使用UITableView Controller
  为了简化操作,推出下面的方法。
  即如果在界面上仅仅只是需要用来展示一个UITableView,那么可以让主控制器直接继承于UITableView Controller
复制代码
 1 //
 2 //  YYViewController.h
 3 //  UITableView Controller
 4 //
 5 //  Created by 孔医己 on 14-6-2.
 6 //  Copyright (c) 2014年 itcast. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface YYViewController : UITableViewController
12 
13 @end
复制代码
 
 直接让控制器继承UITableView controller,然后在storyboard中把以前的界面删掉,拖一个tableview controller就可以了。
 
注意:需要和主控制器类进行关联。
UITableView Controller里面有个tableview属性,在控制器中通过self.view获取出来的视图就是一个tableview。
即self.view=self.taleview。
且它默认已经把他的协议和数据源都已经实现好了,不再需要进行连线。
复制代码
 1 //  UITableViewController.h
 2 //  UIKit
 3 //
 4 //  Copyright (c) 2008-2013, Apple Inc. All rights reserved.
 5 //
 6 #import <Foundation/Foundation.h>
 7 #import <UIKit/UIViewController.h>
 8 #import <UIKit/UITableView.h>
 9 #import <UIKit/UIKitDefines.h>
10 
11 // Creates a table view with the correct dimensions and autoresizing, setting the datasource and delegate to self.
12 // In -viewWillAppear:, it reloads the table's data if it's empty. Otherwise, it deselects all rows (with or without animation) if clearsSelectionOnViewWillAppear is YES.
13 // In -viewDidAppear:, it flashes the table's scroll indicators.
14 // Implements -setEditing:animated: to toggle the editing state of the table.
15 
16 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
17 
18 - (id)initWithStyle:(UITableViewStyle)style;
19 
20 @property(nonatomic,retain) UITableView *tableView;
21 @property(nonatomic) BOOL clearsSelectionOnViewWillAppear NS_AVAILABLE_IOS(3_2); // defaults to YES. If YES, any selection is cleared in viewWillAppear:
22 
23 @property (nonatomic,retain) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(6_0);
24 
25 @end
复制代码
点击右键,可以发现数据源和代理都已经连好了。
 
(应该把继承自uiviewcontroller的控制器干掉,重新拖一个tableview controller,和主控制器进行连线。)

posted on   文顶顶  阅读(24850)  评论(0编辑  收藏  举报

编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示