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 ...
随机推荐
- java_log4j 经典配置
程序加载制定日志文件 public static final String log4j = "log4j.xml"; /** * @declare 加载log4j * @throw ...
- C# 通过ImportNode AppendChild方法合并XmlDocument,XML转为DataTable
var doc1 = new XmlDocument(); var doc2 = new XmlDocument(); XmlNode root1 = doc1.DocumentElement; do ...
- MVC验证07-自定义Model级别验证
原文:MVC验证07-自定义Model级别验证 在一般的自定义验证特性中,我们通过继承ValidationAttribute,实现IClientValidatable,只能完成对某个属性的自定义验证. ...
- OpenGL入门【1 高速入门】
// OpenGL.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include &l ...
- jQuery的ready方法实现原理分析
jQuery中的ready方法实现了当页面加载完成后才执行的效果,但他并不是window.onload或者doucment.onload的封装,而是使用 标准W3C浏览器DOM隐藏api和IE浏览器缺 ...
- 安装WindowsXP操作系统(安装版) - 初学者系列 - 学习者系列文章
本文主要介绍下Windows XP操作系统的安装. 1. 将光驱装入光驱.启动电脑,在开始界面按下DEL键,进入BIOS设置界面.将光驱设置为第一启动项.下面以虚拟机为例子. ...
- VMWare 11安装操作系统 - 初学者系列 - 学习者系列文章
在2010年的时候,我写过一篇关于VMWare的安装操作系统的博文.但是今天在QQ群里有人问起VMWare安装操作系统的问题,虽然回答了,但是回头看了下当时那篇博文,决定重新写一文. 首先要获取VMW ...
- Scala是一门现代的多范式编程语言
Scala是一门现代的多范式编程语言 Scala是一门现代的多范式编程语言,志在以简练.优雅及类型安全的方式来表达常用编程模式.它平滑地集成了面向对象和函数语言的特性. Scala是面向对象的:Sca ...
- 02.零成本实现WEB性能测试-基于APACHE JMETER
书评: 1.这本是介绍性能测试工具Jmeter的书籍,维度还够,但是粒度太粗. 2.对于想快速了解JMeter的使用和工具的原件使用,还是有一定的参考价值. 3.实际上,这本书可用来快速入门,掌握和了 ...
- C#使用Thrift简介,C#客户端和Java服务端相互交互
C#使用Thrift简介,C#客户端和Java服务端相互交互 本文主要介绍两部分内容: C#中使用Thrift简介 用Java创建一个服务端,用C#创建一个客户端通过thrift与其交互. 用纯C#实 ...