ios-表视图-demo7-cell的编辑


//
// RootTableViewController.m
// editcell
//
// Created by liyang on 14-4-29.
// Copyright (c) 2014年 liyang. All rights reserved.
//

#import "RootTableViewController.h"

@interface RootTableViewController ()

@end

@implementation RootTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {

}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

_fontarrary=[NSMutableArray arrayWithArray:[UIFont familyNames]];
self.navigationItem.rightBarButtonItem=self.editButtonItem;//这个是给这个导航控制器加上一个按钮,并且这个按钮还是主动调用了下面-(void)setEditing:(BOOL)editing animated:(BOOL)animated这个方法,这个方法和UITableView有个同名方法,但是是不一样的。调用这个功能就是这个editButtonItem来实现的
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{//这个是继承了父类视图控制器的方法
if (self.tableView.editing) {
[self.tableView setEditing:NO animated:YES];
}else {
[self.tableView setEditing:YES animated:YES];
}
}//使我们的表视图处于编辑或者非编辑状态

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{//这个表示哪些行可以进行编辑(增,删,移动)
NSLog(@"canEditRowAtIndexPath");
if (indexPath.row==0) {
return NO;
}
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{//通过代理方法来进行判断编辑风格

if (indexPath.row==1) {
NSLog(@"UITableViewCellEditingStyleInsert");
return UITableViewCellEditingStyleInsert;
}
NSLog(@"UITableViewCellEditingStyleDelete");

return UITableViewCellEditingStyleDelete;
}//编辑的样式
int count=0;
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_fontarrary removeObjectAtIndex:indexPath.row];//删除的时候要先删除数据
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
NSString *font_new=[NSString stringWithFormat:@"newfont%d",count];
[_fontarrary insertObject:font_new atIndex:indexPath.row+1];
NSIndexPath *_aaindexpath=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
[tableView insertRowsAtIndexPaths:@[_aaindexpath] withRowAnimation:UITableViewRowAnimationRight];
count++;
}
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [_fontarrary count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier];

if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
lable.backgroundColor=[UIColor purpleColor];
lable.tag=101;
[cell.contentView addSubview:lable];
}
UILabel *lable=(UILabel *)[cell.contentView viewWithTag:101];
lable.text=_fontarrary[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{

NSString *text=[_fontarrary objectAtIndex:fromIndexPath.row];
[_fontarrary removeObjectAtIndex:fromIndexPath.row];
[_fontarrary insertObject:text atIndex:toIndexPath.row];
}//移动结束后,为的是修改数据

// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{ NSLog(@"canMoveRowAtIndexPath");
// Return NO if you do not want the item to be re-orderable.
return YES;
}

//总结:首先cell展示出来的时候就会判断这个cell是否可以编辑,然后编辑的时候就会先判断是否可编辑,然后调用判断此cell的编辑风格,然后判断此 cell是否可以移动,咋一看,为毛多此一举,在出现cell的时候就去判断是否可编辑,我想,我认为是出于数据结构中的,空间换取效率吧(纯属揣测),最后完成提交编辑的时候还会调用一次,是否可编辑。

@end

ios-表视图-demo7-cell的编辑,码迷,mamicode.com

时间: 2024-08-06 16:05:55

ios-表视图-demo7-cell的编辑的相关文章

iOS表视图常见问题

Q:表视图只需要部分单元格,怎样删除下方多余的空白单元格? A:代码如下 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return [UIView new]; }

IOS开发中UITableView(表视图)的性能优化及自定义Cell

IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITableView实现的.UITableView这个控件中的列表的每一行是一个cell,当UITableView中cell数量特别大的时候,由于每次都需要alloc分配内存并初始化,会导致app运行不流畅,所以可以使用苹果提供的几个方法进行优化,我把这个过程记录下来供自己以后查阅. 当然,既然说到优化,那我们

IOS 集合视图指南1:介绍

About iOS Collection Views(关于IOS集合视图) A collection view is a way to present an ordered set of data items using a flexible and changeable layout. The most common use for collection views is to present items in a grid-like arrangement, but collection v

IOS开发之表视图爱上CoreData

在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功能和JavaEE中的Hibernate的功能类似,最基本是两者都有通过对实体的操作来实现对数据库的CURD操作.CoreData中的上下文(managedObjectContext)就相当于Hibernate中的session对象, CoreData中的save操作就和Hibernate中的comm

IOS之表视图单元格删除、移动及插入

1.实现单元格的删除,实现效果如下 Cpp代码   - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @"编辑"; self.navigation.rightBarButtonItem = self.editButtonItem; [self initTableViewData]; // Do any additional setup after loading the view

UI_10 表视图的编辑、UITableViewController

读取plist文件并将其内容显示到表视图上.并添加编辑(增加,删除).移动cell的操作. plist文件内容如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" " http://www.apple.com/DTDs/PropertyList-1.0.dtd ">

UI学习笔记---第十天UITableView表视图编辑

UITableView表视图编辑 表视图编辑的使用场景 当我们需要手动添加或者删除某条数据到tableView中的时候,就可以使用tableView编辑.比如微信 扣扣中删除和某人的通话 当我们需要手动调整单元格的顺序时,就可以通过tableView移动,移动单元格到指定位置 代理AppDelegate.m中代码 #import "AppDelegate.h" #import "RootViewController.h" @implementation AppDel

iOS开发之多表视图滑动切换示例(仿&quot;头条&quot;客户端)

好长时间没为大家带来iOS开发干货的东西了,今天给大家分享一个头条新闻客户端各个类别进行切换的一个示例.在Demo中对所需的组件进行的简单封装,在封装的组件中使用的是纯代码的形式,如果想要在项目中进行使用,稍微进行修改即可. 废话少说,先介绍一下功能点,下图是整个Demo的功能点,最上面左边的TabBarButtonItem是用来减少条目的,比如下图有三个按钮,点击减号会减少一个条目.右边的为增加一个条目.点击相应的按钮是切换到对应的表视图上,下方红色的是滑动的指示器,同时支持手势滑动.运行具体

IOS学习之——表视图 给tableViewController添加悬浮窗口

前言 在IOS中,UITableViewController不如UIViewController用的方便,遇到了一个需求:在TableView中添加一个悬浮按钮,不随TableView滑动而滑动.这个需求在UIViewController里面很好实现,给self.view 添加子视图,再把子视图放到最上方即可.可是在表视图控制器中,就很难办,因为控制器中没有作为tableView的父视图的view存在,而把button作为tableView的子视图出现呢,则会随着table的滑动而滑动(⊙﹏⊙b

【IOS】关于处于表视图行单元 contentView 中的 UIButton 短按不产生高亮效果的

在做demo时, 发现 UITableViewCell 中的 UIButton 短按一次看不到默认的变灰效果, 停留稍长才能变灰, 比如twitter官方客户端用户头像, 微博的转评赞三按钮. 微博各类客户端的头像和其他按钮也是这样, 头像很多直接是imageView加手势. 这方面细节处理的最好的是 Instagram 不管短按长按都会灰, 这样才有打击感. 经一番搜索之后得知 UITableView 做为 UIScrollView 的子类,拥有 delaysContentTouches 的布