zoukankan      html  css  js  c++  java
  • 2015/10/4 iOS 笔记 细节 简单-代理过程 UITableView

    一、简单-代理过程

    1,创建代理

    @class TgFootView;

    @protocol TgFootViewDelegate <NSObject>

    @optional   可选是否实现

     视图的下载按钮被点击

    - (void)tgFootViewDidDownloadButtonClick:(TgFootView *)footView;

     @end

     @interface TgFootView : UIView

     代理如果使用强引用,就会产生循环引用,造成控制器和子视图都无法被释放,造成内存泄露。

    @property (nonatomic,weak) id <TgFootViewDelegate> delegate;

    @end

    2,遵守协议

    <TgFootViewDelegate>

    3,设置代理

         视图控制器成为footer的代理

        footer.delegate = self;

    4,控制器实现代理方法

    - (void)tgFootViewDidDownloadButtonClick:(TgFootView *)footView

    {

    }

     5,判断代理是否实现了协议方法

            if ([self.delegate respondsToSelector:@selector(tgFootViewDidDownloadButtonClick:)]) {

         执行            

                [self.delegate tgFootViewDidDownloadButtonClick:self];

            }

    二、UITableView的几个代理方法

    1、 只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!

     UITableViewCellEditingStyleNone,

     UITableViewCellEditingStyleDelete,     删除

     UITableViewCellEditingStyleInsert      添加

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

    {

        if (editingStyle == UITableViewCellEditingStyleDelete) {

            NSLog(@"要删除");

            

             MVC => 数据是保存在模型中

             1. 删除self.dataList中indexPath对应的数据

            [self.dataList removeObjectAtIndex:indexPath.row];

            NSLog(@"%@", self.dataList);

            

             2. 刷新表格(重新加载数据)

             重新加载所有数据

            [self.tableView reloadData];

             deleteRowsAtIndexPaths让表格控件动画删除指定的行

            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

        } else if (editingStyle == UITableViewCellEditingStyleInsert) {

            NSLog(@"要添加数据");

            

             1. 向数组添加数据

            [self.dataList insertObject:@"王小二" atIndex:indexPath.row + 1];

             2. 刷新表格

            [self.tableView reloadData];

             insertRowsAtIndexPaths让表格控件动画在指定indexPath添加指定行

             新建一个indexPath

            NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];

            [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];

        }

    }

     2、只要实现此方法,就可以显示拖动控件

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

    {

         界面数据UITableView已经完成了

         调整数据即可

        [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

         1. 将源从数组中取出

        id source = self.dataList[sourceIndexPath.row];

         2. 将源从数组中删除

        [self.dataList removeObjectAtIndex:sourceIndexPath.row];

        NSLog(@"%@", self.dataList);

        

         3. 将源插入到数组中的目标位置

        [self.dataList insertObject:source atIndex:destinationIndexPath.row];

        

        NSLog(@"%@", self.dataList);

    }

    3、 返回编辑样式,如果没有实现此方法,默认都是删除

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

    {

        if (indexPath.row % 2) {

            return UITableViewCellEditingStyleInsert;

        } else {

            return UITableViewCellEditingStyleDelete;

        }

        return UITableViewCellEditingStyleInsert;

    }

    4,cell被选中或者取消选中时都会被调用

     如果是自定义cell控件,所有的子控件都应该添加到contentView中

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {

        [super setSelected:selected animated:animated];

        // Configure the view for the selected state

        

        if (selected) {

            self.contentView.backgroundColor = [UIColor redColor];

         } else {

           self.contentView.backgroundColor = [UIColor greenColor];

        }

        

    }

    5, 刷新数据

      1,  [self.tableView reloadData];

       2,  新建一个indexPath

        NSIndexPath *path = [NSIndexPath indexPathForRow:self.tgs.count - 1 inSection:0];

         添加

        [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];

         滚到最后

        [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];

     6,

        self.tableView.tableHeaderView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil] lastObject];

  • 相关阅读:
    JS 实现日期信息增加年数,月数,天数
    ROW_NUMBER() OVER函数的基本用法,也可用于去除重复行
    Oracle存储过程返回游标实例详解
    PL/Sql 中创建、调试、调用存储过程
    HTTP 错误 404.13
    oracle查询多行数据合并成一行数据
    C# 实现list=list.OrderBy(q=>q.字段名).ToList(); 按多个字段排序
    [bcc32 Error] ws2def.h(231): E2238 Multiple declaration for 'sockaddr'
    [bcc32 Error] typeinfo.h(154): E2367 Can't inherit RTTI class from non-RTTI base 'exception'
    sql server 语法 MSDN
  • 原文地址:https://www.cnblogs.com/pjl0426/p/4855266.html
Copyright © 2011-2022 走看看