UITableView的使用和点击每个cell跳转到其他controller优化
2015-08-31 17:07
501 查看
一、需要一个NSObject类,该类代码如下
#import <Foundation/Foundation.h>
@interface MJSampleIndex :
NSObject
@property (nonatomic,
copy) NSString *title; //描述该类的title
@property (nonatomic,
assign) Class controllerClass; //跳转到的controller
+ (instancetype)sampleIndexWithTitle:(NSString *)title controllerClass:(Class)controllerClass; //初始化title和controller
@end
#import "MJSampleIndex.h"
@implementation MJSampleIndex
+ (instancetype)sampleIndexWithTitle:(NSString *)title controllerClass:(Class)controllerClass
{
MJSampleIndex *si = [[self
alloc] init];
si.title = title;
si.controllerClass = controllerClass;
return si;
}
@end
二、UITableViewController方法
实现三个方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//tableview总行数
{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath //根据行数绘制tableviewcell
{
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//点击每个cell执行的事件
{
}
#import "MJSampleIndexViewController.h"
#import "MJSampleIndex.h"
#import "MJTableViewController.h"
#import "MJCollectionViewController.h"
NSString *const MJSampleIndexCellIdentifier =
@"Cell";
@interface MJSampleIndexViewController ()
{
NSArray *_sampleIndexs;
}
@end
@implementation MJSampleIndexViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
self.title =
@"快速集成下拉刷新";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem
alloc] initWithTitle:@"返回"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
// 1.注册cell
[self.tableView
registerClass:[UITableViewCell
class] forCellReuseIdentifier:MJSampleIndexCellIdentifier];
// 2.初始化数据
MJSampleIndex *si1 = [MJSampleIndex
sampleIndexWithTitle:@"tableView刷新演示"
controllerClass:[MJTableViewController
class]];
MJSampleIndex *si2 = [MJSampleIndex
sampleIndexWithTitle:@"collectionView刷新演示"
controllerClass:[MJCollectionViewController
class]];
_sampleIndexs =
@[si1, si2];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
_sampleIndexs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:MJSampleIndexCellIdentifier
forIndexPath:indexPath];
// 1.取出模型
MJSampleIndex *si =
_sampleIndexs[indexPath.row];
// 2.赋值
cell.textLabel.text = si.title;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.取出模型
MJSampleIndex *si =
_sampleIndexs[indexPath.row];
// 2.创建控制器
UIViewController *vc = [[si.controllerClass
alloc] init];
vc.title = si.title;
// 3.跳转
[self.navigationController
pushViewController:vc animated:YES];
}
@end
#import <Foundation/Foundation.h>
@interface MJSampleIndex :
NSObject
@property (nonatomic,
copy) NSString *title; //描述该类的title
@property (nonatomic,
assign) Class controllerClass; //跳转到的controller
+ (instancetype)sampleIndexWithTitle:(NSString *)title controllerClass:(Class)controllerClass; //初始化title和controller
@end
#import "MJSampleIndex.h"
@implementation MJSampleIndex
+ (instancetype)sampleIndexWithTitle:(NSString *)title controllerClass:(Class)controllerClass
{
MJSampleIndex *si = [[self
alloc] init];
si.title = title;
si.controllerClass = controllerClass;
return si;
}
@end
二、UITableViewController方法
实现三个方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//tableview总行数
{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath //根据行数绘制tableviewcell
{
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//点击每个cell执行的事件
{
}
#import "MJSampleIndexViewController.h"
#import "MJSampleIndex.h"
#import "MJTableViewController.h"
#import "MJCollectionViewController.h"
NSString *const MJSampleIndexCellIdentifier =
@"Cell";
@interface MJSampleIndexViewController ()
{
NSArray *_sampleIndexs;
}
@end
@implementation MJSampleIndexViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
self.title =
@"快速集成下拉刷新";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem
alloc] initWithTitle:@"返回"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
// 1.注册cell
[self.tableView
registerClass:[UITableViewCell
class] forCellReuseIdentifier:MJSampleIndexCellIdentifier];
// 2.初始化数据
MJSampleIndex *si1 = [MJSampleIndex
sampleIndexWithTitle:@"tableView刷新演示"
controllerClass:[MJTableViewController
class]];
MJSampleIndex *si2 = [MJSampleIndex
sampleIndexWithTitle:@"collectionView刷新演示"
controllerClass:[MJCollectionViewController
class]];
_sampleIndexs =
@[si1, si2];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
_sampleIndexs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:MJSampleIndexCellIdentifier
forIndexPath:indexPath];
// 1.取出模型
MJSampleIndex *si =
_sampleIndexs[indexPath.row];
// 2.赋值
cell.textLabel.text = si.title;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.取出模型
MJSampleIndex *si =
_sampleIndexs[indexPath.row];
// 2.创建控制器
UIViewController *vc = [[si.controllerClass
alloc] init];
vc.title = si.title;
// 3.跳转
[self.navigationController
pushViewController:vc animated:YES];
}
@end
相关文章推荐
- UIAlertView在其他页面快速显示?
- poj 3080 Blue Jeans
- 关于UIBarItem和UINvigationController,UITabBarController关系
- IE7/8当文本模式为杂项"Quirks" 导致JSP页面变样的问题
- r.js 打包压缩文件(依赖require)
- hdoj 4062 Queuing 【矩阵快速幂优化递推公式】
- UITableView-------模型的使用
- 你给我解析清楚,都有了CALayer了,为什么还要UIView
- 用Arduino监控的自动咖啡壶
- Android线程---UI线程和非UI线程之间通信
- UIEvent - 1
- 77 NSMutableURLRequest常用设置 GET和POST比较 发送JSON给服务器
- 阿里DruidDataSource配置(druid)
- 疯狂ios讲义之选择器(UIPickerView)
- 一个实用的android框架(二)—— UI
- iOS_UI_5_user-defined(自定义)View
- UINavigationController官方文档学习笔记
- poj 1679 The Unique MST
- DataGridView Index -1 does not have a value 错误
- SoapUI 引用第三方jar包和引用Groovy脚本