• iOS UI 07 uitableviewi3


    //

    //  RootViewController.m

    //  ui - 09  通讯录

    //

    //  Created by dllo on 15/11/19.

    //  Copyright (c) 2015 dllo. All rights reserved.

    //


    #import "RootViewController.h"

    #import "Contact.h"

    @interface RootViewController () <UITableViewDelegate , UITableViewDataSource>

    @property (nonatomic, retain) NSMutableDictionary *contactDic;

    @property (nonatomic, retain) UITableView *tablev;

    @end


    @implementation RootViewController

    - (void)dealloc

    {

        [_contactDic release];

        [super dealloc];

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        self.view.backgroundColor = [UIColor whiteColor];

        self.navigationItem.title = @"通讯录";

        

        [self getData];

        

        self.tablev = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

        self.tablev.delegate = self;

        self.tablev.dataSource = self;

        [self.view addSubview:self.tablev];

        [_tablev release];

    #warning 编辑 - 添加编辑键

        self.navigationItem.rightBarButtonItem = self.editButtonItem;

        


        

        

        

        // Do any additional setup after loading the view.

    }

    #warning 编辑2 - tableview处于编辑状态

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated

    {

        [super setEditing:editing animated:animated];

        //当点击编辑的时候table也进入编辑状态

        [self.tablev setEditing:editing animated:animated];

        

        

    }

    #warning 编辑3 指定tableview哪些行可以编辑

    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return YES;

    }

    #warning 编辑4 - 指定tableview编辑的样式(添加, 删除)

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if (indexPath.section < 4) {

            return UITableViewCellEditingStyleDelete;

        } else {

            return UITableViewCellEditingStyleInsert;

        }

    }

    #warning 编辑5 编辑完成(先操作数据源,再修改UI)

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    {

        NSString *key = [self.contactDic.allKeys objectAtIndex:indexPath.section];

        NSMutableArray *Arr = [self.contactDic objectForKey:key];

        //删除操作

        if (UITableViewCellEditingStyleDelete == editingStyle) {

            //先删数据

            [Arr removeObjectAtIndex:indexPath.row];

            //再改UI

            //局部修改 - 只修改指定行

    //        [self.tablev deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

            //全局修改 - 重新加载一遍

            [self.tablev reloadData];

        }

        if (UITableViewCellEditingStyleInsert == editingStyle) {

            //先改数据

            Contact *cta = [[Contact alloc]init];

            cta.name = @"王宁";

            cta.gender = @"";

            cta.phoneNumber = @"54765735";

            cta.age = @"23";

            cta.hobby= @"qingchung";

            [Arr insertObject:cta atIndex:indexPath.row];

            [cta release];

            //再改ui

            [self.tablev insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

        }

        

        

        

    }

    #warning 移动1 -参照编辑的添加编辑键

    #warning 移动 2 - 参照编辑的让tableview处于编辑状态

    #warning 移动3 - 指定table哪些行可以移动

    - (bool)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

    {

        

        return YES;

    }

    #warning 移动4 移动完成 (移动功能的逻辑)

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

    {

        NSString *key = [self.contactDic.allKeys objectAtIndex:sourceIndexPath.section];

        NSMutableArray *Arr = [self.contactDic objectForKey:key];

        

        //根据原地址找到待操作的contact对象

        //注意, 手动进行引用计数加1的操作, 防止从数组数组移除后因为计数减1引起空间释放

        Contact *contact = [[Arr objectAtIndex:sourceIndexPath.row] retain];

        [Arr removeObjectAtIndex:sourceIndexPath.row];

        //找到移动的目的地

         NSString *key2 = [self.contactDic.allKeys objectAtIndex:destinationIndexPath.section];

        NSMutableArray *Arr2 = [self.contactDic objectForKey:key2];

        [Arr2 insertObject:contact atIndex:destinationIndexPath.row];

        //不要忘记release 对应手动retain

        [contact release];

        

        //再改ui - 注意应用局部改变时要注意原来的位置是否还有数据

    //    [self.tablev moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath]

        [self.tablev reloadData];

        

    }


    #warning 移动5 检测移动过程 实现限制跨区移动

    - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

    {

        //若目的区与原区不同,则返回原地址不允许移动

        if (sourceIndexPath.section != proposedDestinationIndexPath.section) {

            return sourceIndexPath;

        } else {

            return proposedDestinationIndexPath;

        }

    }





    - (void)getData

    {

        self.contactDic = [NSMutableDictionary dictionary];

        NSString *filepath = [[NSBundle mainBundle] pathForResource:@"contact" ofType:@"plist"];

        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filepath];

        for (NSString *key in dic) {

            NSMutableArray *Arr = [NSMutableArray array];

            for (NSMutableDictionary *temp in [dic objectForKey:key]) {

                Contact *contact = [[Contact alloc]init];

    //            contact.name = [temp objectForKey:@"name"];

    //            contact.gender = [temp objectForKey:@"gender"];

    //            contact.age = [temp  objectForKey:@"age"];

    //            contact.phoneNumber = [temp objectForKey:@"phoneNumber"];

    //            contact.hobby = [temp objectForKey:@"hobby"];

    //            contact.picture = [temp objectForKey:@"picture"];

                //上面的这这一大段可以用一句话代替

               [contact setValuesForKeysWithDictionary:temp];

                [Arr addObject:contact];

                [contact release];

            }

            [self.contactDic setObject:Arr forKey:key];

        }

    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return 100;

    }

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

    {

        return [self.contactDic.allKeys objectAtIndex:section];

    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return self.contactDic.allKeys.count;

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        NSString *key = [self.contactDic.allKeys objectAtIndex:section];

        NSMutableArray *Arr = [self.contactDic objectForKey:key];

        return Arr.count;

    }




    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        NSString *cellstr = @"cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellstr];

        if (nil == cell) {

            cell = [[[UITableViewCell alloc]initWithStyle:      UITableViewCellStyleSubtitle reuseIdentifier:cellstr] autorelease];

        }

        NSString *key = [self.contactDic.allKeys objectAtIndex:indexPath.section];

        NSMutableArray *arr = [self.contactDic objectForKey:key];


        Contact *contact = [arr objectAtIndex:indexPath.row];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.textLabel.text = contact.name;

        cell.detailTextLabel.text = contact.phoneNumber;

        return cell;


    }

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

    {

        return self.contactDic.allKeys;

    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        NSString *key = [self.contactDic.allKeys objectAtIndex:indexPath.section];

        NSMutableArray *Arr = [self.contactDic objectForKey:key];

        Contact *contact = [Arr objectAtIndex:indexPath.row];

        

        

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }


    /*

    #pragma mark - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */


    @end



  • 相关阅读:
    用email实现邮件模板
    jquery.transform
    学习 表单验证插件validate
    倒计时
    时间
    听慕课学自定义滚动条
    css3动画、边框、投影知识
    sql查询字段值只为汉字(桃)
    sql按照汉字首字母顺序排序(桃)
    poi导出excel文件(桃)
  • 原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5043082.html
Copyright © 2020-2023  润新知