IOS学习之路六(UITableView滑动删除指定行)
滑动删除指定行代码如下:
Controller.h文件
- #import <UIKit/UIKit.h>
- @interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
- @property (nonatomic, strong) UITableView *myTableView;
- @property(nonatomic,strong) NSMutableArray *arrayOfRows;
- @end
Controller.m文件
- //
- // TableViewController.m
- // UITableViewDemo
- //
- // Created by WildCat on 13-8-6.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import "TableViewController.h"
- @interface TableViewController ()
- @end
- @implementation TableViewController
- @synthesize myTableView;
- @synthesize arrayOfRows;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- self.view.backgroundColor = [UIColor whiteColor];
- myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
- //设置列表样式为简单的样式 还有一个样式为UITableViewStyleGrouped为分组模式 UITableViewStylePlain为普通的样式
- self.myTableView.delegate = self;//设置代理为自身
- self.myTableView.dataSource = self;//设置数据源为自身
- self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
- //确保TablView能够正确的调整大小
- arrayOfRows = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d", nil];//初始化表格数据
- [self.view addSubview:myTableView];
- }
- //设置每行的高度
- -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- CGFloat result = 20.0f;
- if ([tableView isEqual:self.myTableView]) {
- result = 80.0f;
- }
- return result;
- }
- //允许数据源告知必须加载到Table View中的表的Section数。
- //-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- // NSInteger result = 0;
- // if([tableView isEqual:myTableView]){
- // result = 3;//一共三个section
- // }
- // return result;
- //}
- //设置每个Section呈现多少行
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return [self.arrayOfRows count];
- }
- //每行对应的数据
- -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- UITableViewCell *result = nil;
- if ([tableView isEqual:myTableView]) {
- static NSString *tableViewCellIdentifier = @"MyCells";//设置Cell标识
- result = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];//通过标示符返回一个可重用的表视图单元格对象
- if (result == nil) {
- result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier];//初始化一个表格单元格样式和重用的标识符,并将它返回给调用者。
- }
- //indexPath.section 表示section的索引 indexPath.row表示行数的索引
- result.textLabel.text = [self.arrayOfRows objectAtIndex:indexPath.row];
- }
- return result;
- }
- //点击某一行时候触发的事件
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- if ([tableView isEqual:myTableView]) {
- NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",(long)indexPath.row,(long)indexPath.section]);
- }
- }
- //要求委托方的编辑风格在表视图的一个特定的位置。
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
- UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;//默认没有编辑风格
- if ([tableView isEqual:myTableView]) {
- result = UITableViewCellEditingStyleDelete;//设置编辑风格为删除风格
- }
- return result;
- }
- -(void)setEditing:(BOOL)editing animated:(BOOL)animated{//设置是否显示一个可编辑视图的视图控制器。
- [super setEditing:editing animated:animated];
- [self.myTableView setEditing:editing animated:animated];//切换接收者的进入和退出编辑模式。
- }
- -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。
- if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
- if (indexPath.row<[self.arrayOfRows count]) {
- [self.arrayOfRows removeObjectAtIndex:indexPath.row];//移除数据源的数据
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的数据
- }
- }
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // self.myTableView = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- @end
执行截图:
转载请注明:
新浪微博:http://weibo.com/u/3202802157
IOS学习之路六(UITableView滑动删除指定行)的更多相关文章
- iOS学习笔记之UITableViewController&UITableView
iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...
- IOS开发---菜鸟学习之路--(二十二)-近期感想以及我的IOS学习之路
在不知不觉当中已经写了21篇内容 其实一开始是没有想些什么东西的 只是买了Air后 感觉用着挺舒服的,每天可以躺在床上,就一台笔记本,不用网线,不用电源,不用鼠标,不用键盘,干干脆脆的就一台笔记本. ...
- iOS学习笔记(4) — UITableView的 重用机制
iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...
- sed 删除最后几行 和删除指定行 awk使用
sed 删除最后几行 和删除指定行 转载原文链接:http://blog.51cto.com/lspgyy/1305489 sed 想删除文件中的指定行,是可以用行号指定也可以用RE来匹配的. 删 ...
- Pandas常用操作 - 删除指定行/指定列
1. 删除指定行 new_df = df.drop(index='行索引') new_df = df.drop('行索引', axis='index') new_df = df.drop('行索引', ...
- 浅谈iOS学习之路(转)
转眼学习iOS已经快两年的时间了,这个路上有挫折也有喜悦,一步步走过来发现这个过程是我这一辈子的财富,我以前的老大总是对我说,年轻就是最大的资本(本人91年),现在才算是慢慢的体会到,反观自己走过的这 ...
- 浅谈iOS学习之路
转眼学习iOS已经快两年的时间了,这个路上有挫折也有喜悦,一步步走过来发现这个过程是我这一辈子的财富,我以前的老大总是对我说,年轻就是最大的资本(本人91年),现在才算是慢慢的体会到,反观自己走过的这 ...
- [Xcode 实际操作]五、使用表格-(6)UITableView滑动到指定单元格
目录:[Swift]Xcode实际操作 本文将演示如何使表格滑动到指定的索引路径. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首 ...
- REDHAT一总复习1 vim编辑器的使用 删除所有者列 删除指定行
将文件/home/student/vimfile.txt 复制到server 上的/home/student/longlisting.txt . 根据下列要求,使用vim编辑器更改 /home/stu ...
随机推荐
- mysql_【MySQL】常见的mysql 进程state
Analyzing 线程是对MyISAM 表的统计信息做分析(例如, ANALYZE TABLE ). checking permissions 线程是检查服务器是否具有所需的权限来执行该语句. Ch ...
- linux_Ubuntu 12.04 安装jdk
1.下载jdk6jdk6下载地址为:http://download.java.net/jdk6/,根据操作系统的选择对应的安装包,我的是ubuntu 12.04 32bit的,所以下载的文件是jdk- ...
- Java数据结构与算法(4) - ch04队列(Queue和PriorityQ)
队列: 先进先出(FIFO). 优先级队列: 在优先级队列中,数据项按照关键字的值有序,关键字最小的数据项总在对头,数据项插入的时候会按照顺序插入到合适的位置以确保队列的顺序,从后往前将小于插入项的数 ...
- Linux下javaweb
Linux下javaweb环境搭建 步骤: 1.使用远程工具连接上服务器,例如xsheel(ssh).filezilla(ftp) 2.JDK安装及相关配置 3.Mysql安装及相关配置 4.Tomc ...
- MyEclipse使用总结——MyEclipse文件查找技巧
原文:MyEclipse使用总结--MyEclipse文件查找技巧 一.查找文件 使用快捷键[ctrl+shift+R]弹出弹出文件查找框,如下图所示: 二.查找包含某个字符串的文件 使用快捷键[ct ...
- php传引用和全局变量
原文:php传引用和全局变量 <?php /* * 函数内部改变变量的值两种方法 */ $a = 10; /* *方法一 :函数参数传引用 */ function methodOne(& ...
- MyReport报表引擎2.7.6.7新功能
新增二维码控件PDF417 设计器新增数据选项卡,可以拖放字段进行绑定 相关链接 MyReport演示.产品站点 相关文章 MyReport专栏
- visual studio 辅助工具
resharper 这是一个收费软件 需要下载对应版本的注册机. 效果如图所示: 这里的using 很多没有用到,他会用灰色标记,你都可以统统去掉. 声明一个类 ,要求 首字母大写,如果你小写了,他 ...
- Equals 和==
class Person { private string name; public string Name { get ...
- Linux server关闭自己主动
公司linux server发生错误.mysql server没有理由关闭,我找不到理由.Version: '5.6.13-enterprise-commercial-advanced' socket ...