年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2475|回复: 0

[实用控件] ios实战开发之UITableView应用(联系人管理)

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-4-2 17:56:52 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2014-4-3 23:08 编辑

    1、效果展示:



    2、过程分析:

    2.1 用storyboard创建好基本界面

    QQ20140403-1@2x.png

    2.2 编写一个“编辑”按钮事件,该事件让UITableView进入编辑模式
    2.3 实现UITableViewDelegate协议中UITableViewCell的移动与删除事件
    1. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    2. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    复制代码

    3、关键代码:
    SAPerson.h
    1. //
    2. //  SAPerson.h
    3. //  UItableView-4
    4. //
    5. //  Created by yusian on 14-4-2.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @interface SAPerson : NSObject
    10. @property (nonatomic, copy) NSString *name;
    11. @property (nonatomic, copy) NSString *phone;
    12. @end
    复制代码
    SAPerson.m
    1. //
    2. //  SAPerson.m
    3. //  UItableView-4
    4. //
    5. //  Created by yusian on 14-4-2.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "SAPerson.h"
    9. @implementation SAPerson
    10. @end
    复制代码
    SAViewController.h
    1. //
    2. //  SAViewController.h
    3. //  UItableView-4
    4. //
    5. //  Created by yusian on 14-4-2.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <UIKit/UIKit.h>
    9. #import "SAPerson.h"
    10. @interface SAViewController : UIViewController
    11. - (IBAction)edit:(UIBarButtonItem *)sender;
    12. @property (weak, nonatomic) IBOutlet UITableView *tableView;
    13. @end
    复制代码
    SAViewController.m
    1. //
    2. //  SAViewController.m
    3. //  UItableView-4
    4. //
    5. //  Created by yusian on 14-4-2.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "SAViewController.h"
    9. @interface SAViewController () <UITableViewDataSource, UITableViewDelegate>
    10. {
    11.     NSMutableArray *_modelData;
    12. }
    13. @end
    14. @implementation SAViewController
    15. - (void)viewDidLoad
    16. {
    17.     [super viewDidLoad];
    18.    
    19.     // 初始化模型数组,将当前控制器设置为数据源/代理
    20.     _modelData = [NSMutableArray array];
    21.     self.tableView.dataSource = self;
    22.     self.tableView.delegate = self;
    23.    
    24.     for (int i = 0; i < 20; i++) {
    25.    
    26.         // 初始化模型数据,模型数据包括(姓名  电话号码),姓名按0~20加序号,手机号码通过随机函数产生(仅做演示)
    27.         SAPerson *p = [[SAPerson alloc] init];
    28.         p.name = [NSString stringWithFormat:@"姓名-%02d", i];
    29.         p.phone = [NSString stringWithFormat:@"手机:187%d", 10000000 + arc4random_uniform(90000000)];
    30.         [_modelData addObject:p];
    31.     }
    32. }
    33. #pragma mark 返回表格行数--UITableViewDataSource协议必须实现
    34. - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    35. {
    36.     return _modelData.count;    // 数据模式中数据量即为表格cell个数
    37. }
    38. #pragma mark 初始化Cell--UITableViewDataSource协议必须实现
    39. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    40. {
    41.     SAPerson *p = _modelData[indexPath.row];                                                            // 创建一个模型对象
    42.     static NSString *ID = @"cell";
    43.     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:ID];                      // 回收缓存中Cell
    44.     if (nil == cell) {
    45.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];   // 如果缓存中没有则创建
    46.     }
    47.     cell.textLabel.text = p.name;           // 给cell中属性赋值
    48.     cell.detailTextLabel.text = p.phone;    // 给cell中属性赋值
    49.     return cell;
    50. }
    51. #pragma mark 编辑模式中删除按钮事件处理--UITableViewDelegate协议实现
    52. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    53. {
    54.     SAPerson *p = _modelData[indexPath.row];
    55.     [_modelData removeObject:p];        // 删除当前行数据模型中的数据
    56.     [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];   // 局部刷新,动画处理删除当前cell
    57. }
    58. #pragma mark 编辑模式中移动cell事件处理
    59. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    60. {
    61.     SAPerson *p = _modelData[sourceIndexPath.row];                  // 取出当前行的模型数据
    62.     [_modelData removeObject:p];                                    // 删除当前行的模型数据
    63.     [_modelData insertObject:p atIndex:destinationIndexPath.row];   // 将取出的模型数据插入移动后模型数据位置
    64.     // 该方法调用后会自动刷新表格
    65. }
    66. #pragma mark 点击“编辑”按钮事件处理
    67. - (IBAction)edit:(UIBarButtonItem *)sender {
    68.    
    69.     BOOL result = !self.tableView.editing;              // UITableView编辑模式状态取反再赋值给编辑状态,可实现该方法循环打开或关闭编辑模式
    70.    
    71.     [self.tableView setEditing:result animated:YES];    // 修改UITableView的编辑模式,打开或关闭
    72.    
    73. }
    74. @end
    复制代码
    4、源代码下载:
    游客,本帖隐藏的内容需要积分高于 1 才可浏览,您当前积分为 0
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-27 17:39 , Processed in 0.053564 second(s), 22 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表