码迷,mamicode.com
首页 > 移动开发 > 详细

2015/10/4 iOS 笔记 细节 简单-代理过程 UITableView

时间:2015-10-05 01:54:13      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

 

一、简单-代理过程

 

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];

 

2015/10/4 iOS 笔记 细节 简单-代理过程 UITableView

标签:

原文地址:http://www.cnblogs.com/pjl0426/p/4855266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!