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

时间: 2024-08-01 10:15:45

2015/10/4 iOS 笔记 细节 简单-代理过程 UITableView的相关文章

2015/10/3 iOS 笔记 细节 iOS9中UIAlertController的简单使用 ScrollView NSTimer

一.iOS9中UIAlertController的简单使用 很明显,简单的UIAlertView已经不能用了,我感觉很伤心. // 创建 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"开始了" message:@"开始了!" preferredStyle:UIAlertControllerStyleActionSheet]; // UIAlertControllerS

2015/10/6 iOS 笔记 细节 应用中常见文件

1,工程名-info.plist文件 bundle display name 应用显示的名称(10到12个字符,超过显示...) bundle identifier  应用的唯一标识 com.xx.hhxx bundle version 软件版本号 supported interface orientation 屏幕旋转 默认支持三种模式 2,应用中常见文件 工程名-Prefix.pch (新版没有这个文件了) pch头文件的内容能被项目中得其他所有源文件共享和访问 一般在pch头文件中定义一些

2015/10/2 iOS笔记 细节

一.按钮不能交互的几种情况 1,alpha <= 0.01 (0.02就能点了) 2,hidden = YES 3, userInteraction = NO 4, 所在的父视图不允许交互,按钮也不能交互 5,在父视图可见范围内可以交互,超出范围的部分不能交互. 二.UIImageView 默认 不允许用户交互的. 三.乱序 - (void)randomOptions { // 对option数组乱序 [self.options sortedArrayUsingComparator:^NSCom

IOS笔记044-通知和代理(观察者模式和代理模式)

  处理文本输入框的输入事件,单击文本输入框后要弹出键盘. 弹出键盘有两种实现方式:一种代理,一种通知.也就是对应的(观察者模式和代理模式).   1.通知 1.1.准备工作 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信. 任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么. 其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知.  

兼容iOS 10 资料整理笔记-b

原文链接:http://www.jianshu.com/p/0cc7aad638d9 1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserNotifications的易用,功能也变得非常强大. iOS 9 以前的通知 1.在调用方法时,有些方法让人很难区分,容易写错方法,这让开发者有时候很苦恼. 2.应用在运行时和非运行时捕获通知的路径还不一致. 3.

兼容iOS 10 资料整理笔记

1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserNotifications的易用,功能也变得非常强大. iOS 9 以前的通知 1.在调用方法时,有些方法让人很难区分,容易写错方法,这让开发者有时候很苦恼. 2.应用在运行时和非运行时捕获通知的路径还不一致. 3.应用在前台时,是无法直接显示远程通知,还需要进一步处理. 4.已经发出的通知是不能更新

我关注的一周技术动态 2015.10.04

分布式系统实践 1. Distributed Systems(电子书) http://www.printfriendly.com/print/v2?url=http://book.mixu.net/distsys/ebook.html# 要点: 免费的介绍分布式系统理论的电子书, 这本书的难度非常适合初学者, 涵盖了分布式系统的方方面面, 但是又没有深入细节而无法理解, 结合具体例子, 让分布式理论学起来也不那么枯燥了. 2. 分布式系统一致性的发展历史(一) http://www.dianro

洗澡时感想(2015.10.2篇)

洗澡时感想(2015.10.2篇),以这个为题,或许是自己平时没有其他时间去思考自己吧,唯有冲凉时,身边只有一个水桶,洗发水和香皂(或者沐浴露),还有水龙头的水急冲冲的流着.这个时刻,没有网络,没有虚拟的食物来干扰,除了心里一直想着,可能会出现鬼...  所以洗头发不敢闭着眼睛,擦身子也是神速,两分钟搞点,在自己有时间戳背面的时候,会发现有一层厚厚的泥垢. 蹲着浴室里面,看着水慢慢的流着,手拉着毛巾,毛巾带着水,慢慢的往身上浇水一般,想到我自己目前后悔自己拥有的自行车和台式机,因为我觉得一个人在

张高兴的 Windows 10 IoT 开发笔记:使用 ADS1115 读取模拟信号

原文:张高兴的 Windows 10 IoT 开发笔记:使用 ADS1115 读取模拟信号 考虑到 Raspberry Pi 读取模拟信号是很烦人的事情,更何况是在没人玩的 Windows 10 IoT 下,所以准备正儿八经的写点东西. 需求:使用 Raspberry Pi 读取输出模拟信号的 MQ 系列气体传感器.(GitHub:https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/ADS1115) 由于 Raspberry