UITableView中Cell和section的插入与删除

插入段:

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

插入行:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

如果在编辑模式下,复写此方法:

 1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3     if (editingStyle == UITableViewCellEditingStyleDelete) {
 4         // Delete the row from the data source
 5         if (indexPath.row == 0) {
 6             [self.modeInfomation removeObjectAtIndex:indexPath.section];
 7             [self.modeNameArr removeObjectAtIndex:indexPath.section];
 8             NSIndexSet *sectionIndex = [NSIndexSet indexSetWithIndex:indexPath.section];
 9             [self.tableView deleteSections:sectionIndex withRowAnimation:UITableViewRowAnimationLeft];
10         }
11         else {
12             [[self.modeInfomation objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
13             [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
14         }
15     }
16     else if (editingStyle == UITableViewCellEditingStyleInsert) {
17         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
18     }
19 }

在普通模式下:

1 [self.tableView beginUpdates];
2     [[self.modeInfomation objectAtIndex:tag] addObject:@"hehe"];
3     NSIndexPath *indexpath = [NSIndexPath indexPathForRow:row + 1 inSection:tag];
4     [self.tableView insertRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationRight];
5     [self.tableView endUpdates];

注意:

插入或者删除行或段,先更新datasource,再insertRows或insertSections!两个都要更新!

UITableView中Cell和section的插入与删除

时间: 2024-08-25 03:59:22

UITableView中Cell和section的插入与删除的相关文章

UITableView中cell边框和背景设置最佳方案

UITableView是iOS开发中最常用的视图控件,在平常用的iOS App中大部分都用到了UITableView. 需求很简单,就是在一个UITableView里面实现一个不一样的UITableViewCell,如下图里的“切换账号”按钮 正常情况下grouped样式(UITableViewStyleGrouped)UITableViewCell都是有边框的,所以如果只是用addSubView添加一个按钮的话,就会有边框在外面,不符合要求,也想过用一个大的图片,把这个cell给盖住,但是感觉

[2.1] D3.js中关于如何选择,插入,删除元素

对D3.js或数据可视化有兴趣的朋友欢迎到 www.ourd3js.com 讨论,本人博客首页为: http://blog.csdn.net/lzhlzz ,转载请注明出处,谢谢. 在D3.js中,选择元素的函数有两个:select 和 selectAll . 先说明一下它们的区别: select 是选择所有指定元素的第一个 selectAll 是选择指定元素的全部(以用于后面同时操作) 来看一个具体的例子,现有如下代码: <html> <head> <meta charse

用适配器模式处理复杂的UITableView中cell的业务逻辑

适配器是用来隔离数据源对cell布局影响而使用的,cell只接受适配器的数据,而不会与外部数据源进行交互. 源码: ModelCell.h 与 ModelCell.m // // ModelCell.h // Adapter // // Created by XianMingYou on 15/2/10. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import <UIKit/UIKit.h> #define NAM

UITableView中cell里的UITextField不被弹出键盘挡住

效果如下: 源码: EditCell.h 与 EditCell.m // // EditCell.h // Cell // // Created by YouXianMing on 14/12/18. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @interface EditCell : UITableViewCell @property (nonatomic, stro

UITableView中cell点击的绚丽动画效果

本人视频教程系类   iOS中CALayer的使用 效果图: 源码: YouXianMingCell.h 与 YouXianMingCell.m // // YouXianMingCell.h // CellAnimation // // Created by YouXianMing on 14/12/27. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @interfac

解决UITableView中Cell重用机制导致内容出错的方法总结

UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点击事件,也可以在UITableViewCell中加入 UITextField或者UITextView等子视图,使得可以在cell上进行文字编辑. UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符 (reuseIdentif

iOS如何固定UITableView中cell.imageView.image的图片大小

凡是进行ios开发的,基本上都会遇到要展示列表,或者即使不是标准列表,但由于数量不固定,也需要如同列表一样从上往下显示.加载的情况.这些,都绕不过对UITableView的使用. 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不开它强大的功能. 我们经常在开发过程中会用到默认UITableView的cell.imageView.image,如果图

[IOS开发教程] iOS如何固定UITableView中cell.imageView.image的图片大小

凡是进行ios开发的,基本上都会遇到要展示列表,或者即使不是标准列表,但由于数量不固定,也需要如同列表一样从上往下显示.加载的情况.这些,都绕不过对UITableView的使用. 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不开它强大的功能. 我们经常在开发过程中会用到默认UITableView的cell.imageView.image,如果图

iOS设置UITableView中Cell被默认选中后怎么触发didselect事件

//默认选中某个cell [self.searchResultTV selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone]; //实现该cell的didSelectRowAtIndexPath方法 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0