ios实战开发之UITableView应用(联系人管理)
本帖最后由 Sian 于 2014-4-3 23:08 编辑1、效果展示:
http://player.youku.com/player.php/sid/XNjkzNTYzODky/v.swf
2、过程分析:
2.1 用storyboard创建好基本界面
2.2 编写一个“编辑”按钮事件,该事件让UITableView进入编辑模式
2.3 实现UITableViewDelegate协议中UITableViewCell的移动与删除事件
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
3、关键代码:
SAPerson.h
//
//SAPerson.h
//UItableView-4
//
//Created by yusian on 14-4-2.
//Copyright (c) 2014年 yusian. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface SAPerson : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *phone;
@end
SAPerson.m
//
//SAPerson.m
//UItableView-4
//
//Created by yusian on 14-4-2.
//Copyright (c) 2014年 yusian. All rights reserved.
//
#import "SAPerson.h"
@implementation SAPerson
@end
SAViewController.h
//
//SAViewController.h
//UItableView-4
//
//Created by yusian on 14-4-2.
//Copyright (c) 2014年 yusian. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "SAPerson.h"
@interface SAViewController : UIViewController
- (IBAction)edit:(UIBarButtonItem *)sender;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
SAViewController.m
//
//SAViewController.m
//UItableView-4
//
//Created by yusian on 14-4-2.
//Copyright (c) 2014年 yusian. All rights reserved.
//
#import "SAViewController.h"
@interface SAViewController () <UITableViewDataSource, UITableViewDelegate>
{
NSMutableArray *_modelData;
}
@end
@implementation SAViewController
- (void)viewDidLoad
{
;
// 初始化模型数组,将当前控制器设置为数据源/代理
_modelData = ;
self.tableView.dataSource = self;
self.tableView.delegate = self;
for (int i = 0; i < 20; i++) {
// 初始化模型数据,模型数据包括(姓名电话号码),姓名按0~20加序号,手机号码通过随机函数产生(仅做演示)
SAPerson *p = [ init];
p.name = ;
p.phone = ;
;
}
}
#pragma mark 返回表格行数--UITableViewDataSource协议必须实现
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _modelData.count; // 数据模式中数据量即为表格cell个数
}
#pragma mark 初始化Cell--UITableViewDataSource协议必须实现
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SAPerson *p = _modelData; // 创建一个模型对象
static NSString *ID = @"cell";
UITableViewCell *cell = ; // 回收缓存中Cell
if (nil == cell) {
cell = [ initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID]; // 如果缓存中没有则创建
}
cell.textLabel.text = p.name; // 给cell中属性赋值
cell.detailTextLabel.text = p.phone; // 给cell中属性赋值
return cell;
}
#pragma mark 编辑模式中删除按钮事件处理--UITableViewDelegate协议实现
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
SAPerson *p = _modelData;
; // 删除当前行数据模型中的数据
withRowAnimation:UITableViewRowAnimationTop]; // 局部刷新,动画处理删除当前cell
}
#pragma mark 编辑模式中移动cell事件处理
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
SAPerson *p = _modelData; // 取出当前行的模型数据
; // 删除当前行的模型数据
; // 将取出的模型数据插入移动后模型数据位置
// 该方法调用后会自动刷新表格
}
#pragma mark 点击“编辑”按钮事件处理
- (IBAction)edit:(UIBarButtonItem *)sender {
BOOL result = !self.tableView.editing; // UITableView编辑模式状态取反再赋值给编辑状态,可实现该方法循环打开或关闭编辑模式
; // 修改UITableView的编辑模式,打开或关闭
}
@end
4、源代码下载:
**** Hidden Message *****
页:
[1]