UITableView 基本使用

1、要使用UITableView必须用当前实现两个协议<UITableViewDataSource, UITableViewDelegate> UITableViewDataSource协议实现了数据加载的方法,UITableViewDelgate协议实现了UITableView外观设置,事件等方法。

01 // RootViewController.h 测试控制器
02
03 #import <UIKit/UIKit.h>
04
05 @interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
06 {
07 @private
08     UITableView *_tableView;
09     NSArray     *_listArray;
10 }
11 @end

_tableView 私有全局变量用来保存 UITableView对象。
_listArray   私有全局变量用来存放 UITableView需要的数据。

2、创建UITableView 

01 - (void)loadView {
02      
03     // 创建一个基本视图
04     UIView  *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
05     // 设置为当前控制器的默认视图
06     self.view = view;
07     // 释放视图
08     [view release];
09      
10     // 创建tableview
11     _tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
12     // 设置数据源 注意这个方法设置不对,没有数据哦!
13     _tableView.dataSource = self;
14     // 添加到默认控制器中
15     [self.view addSubview:_tableView];
16     // tableview 获取系统默认字体数组 retain 是必须的,不然会报错哦!因为_listArray 必须接管这个数组对象
17     _listArray = [[UIFont familyNames] retain];   
18      
19 }
3、数据源方法
01 // 设置行数
02 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
03     return [_listArray count];
04 }
05
06 // 设置行数据
07
08 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
09     static NSString *cellIdentifier = @"cell";
10
11     UITableViewCell  *cell  = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
12      
13     if (cell == nil) {
14         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
15     }
16      
17     NSString *fontName = _listArray[indexPath.row];
18     cell.textLabel.text = fontName;
19     cell.textLabel.textColor = [UIColor blueColor];
20     cell.textLabel.font = [UIFont fontWithName:fontName size:12];
21      
22     return cell;
23 }

01     // 设置表视图的颜色
02    _tableView.backgroundColor = [UIColor yellowColor];
03
04     // 设置表视图的分割线的颜色
05 //    _tableView.separatorColor = [UIColor purpleColor];
06
07     // 设置表视图的分割线的风格
08  _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
09
10     // 设置表视图的头部视图(headView 添加子视图)
11 UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
12  headerView.backgroundColor = [UIColor redColor]
13
14
15     // 添加子视图
16     UILabel *headText = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 200, 80)];
17
18     headText.text = @"天晴朗,天晴朗天晴朗天晴朗!";
19     // 是否自动换行 0 换行,1 自动缩略
20     headText.numberOfLines = 0;
21
22     [headerView addSubview:headText];
23
24     [headText release];
25
26      
27     _tableView.tableHeaderView = headerView;
28
29     [headerView release];
30
31      
32     // 设置表视图的尾部视图(footerView 添加子视图)   
33
34     UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
35
36     footerView.backgroundColor = [UIColor yellowColor];
37
38     _tableView.tableFooterView = footerView;
39
40     [footerView release];

转http://my.oschina.net/wangdk/blog/150291
在iOS开发中,`UITableView`与`MJRefreshHeader`结合可以实现下拉刷新功能。`MJRefreshHeader`是一个第三方库,由Masonry团队维护,用于管理表格视图的头部刷新控件。 以下是使用`MJRefreshHeader`的基本步骤: 1. **添加依赖**: - 首先,你需要在你的项目中导入MJRefresh库。如果你使用的是CocoaPods,可以在你的Podfile中添加`pod 'MJRefresh', '~> 3.1'`然后运行`pod install`。 2. **初始化头视图**: 在`UITableViewDataSource`的`numberOfSectionsInTableView:`和`tableView:numberOfRowsInSection:`方法中,创建并返回一个`MJRefreshNormalHeader`实例,并将其设置为tableview的header view。 ```swift let header = MJRefreshNormalHeader() yourTableView.header = header ``` 3. **配置刷新状态**: 设置刷新监听器,当用户开始拖动刷新头时,会触发刷新操作。通常在`scrollViewDidScroll:`方法中处理: ```swift override func scrollViewDidScroll(_ scrollView: UIScrollView) { if let refreshHeaderView = yourTableView.header as? MJRefreshHeader { refreshHeaderView自动结束刷新时间 = 0.5 // 设置默认的刷新间隔时间 refreshHeaderView.scrollView = yourTableView refreshHeaderView.state = .pulling // 当用户开始拖拽时进入"正在加载"状态 } } ``` 4. **响应刷新请求**: 在`MJRefreshHeaderDelegate`代理方法`willBeginRefreshing`中,编写你的实际数据加载逻辑: ```swift header?.addTarget(self, action: #selector(loadData), for: .refreshing) @objc func loadData() { // 这里应该替换为实际的数据加载请求,比如网络请求或本地数据更新 DispatchQueue.main.asyncAfter(deadline: .now() + 2) { // 模拟异步加载延迟 yourTableView.reloadData() header?.endRefreshing() // 刷新结束后移除刷新状态 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值