iOS-UI控件之UITableView(四)- cell数据刷新

TableView- 数据刷新

数据刷新

  • 添加数据
  • 删除数据
  • 更改数据

全局刷新方法(最常用)

[self.tableView reloadData];
// 屏幕上的所有可视的cell都会刷新一遍

局部刷新方法

  • 添加数据
NSArray *indexPaths = @[
                        [NSIndexPath indexPathForRow:0 inSection:0],
                        [NSIndexPath indexPathForRow:1 inSection:0]
                        ];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
  • 删除数据
NSArray *indexPaths = @[
                        [NSIndexPath indexPathForRow:0 inSection:0],
                        [NSIndexPath indexPathForRow:1 inSection:0]
                        ];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
  • 更新数据(没有添加和删除数据,仅仅是修改已经存在的数据)
NSArray *indexPaths = @[
                        [NSIndexPath indexPathForRow:0 inSection:0],
                        [NSIndexPath indexPathForRow:1 inSection:0]
                        ];
[self.tableView relaodRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];

左滑出现删除按钮

  • 需要实现tableView的代理方法
/**
 *  只要实现了这个方法,左滑出现Delete按钮的功能就有了
 *  点击了“左滑出现的Delete按钮”会调用这个方法
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 删除模型
    [self.wineArray removeObjectAtIndex:indexPath.row];

    // 刷新
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}

/**
 *  修改Delete按钮文字为“删除”
 */
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

左滑出现N个按钮

  • 需要实现tableView的代理方法
/**
 *  只要实现了这个方法,左滑出现按钮的功能就有了
 (一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES)
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

}

/**
 *  左滑cell时出现什么按钮
 */
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了关注");

        // 收回左滑出现的按钮(退出编辑模式)
        tableView.editing = NO;
    }];

    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [self.wineArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];

    return @[action1, action0];
}

进入编辑模式

// self.tabelView.editing = YES;
//加动画
[self.tableView setEditing:YES animated:YES];
// 默认情况下,进入编辑模式时,左边会出现一排红色的“减号”按钮

在编辑模式中多选

// 编辑模式的时候可以多选
self.tableView.allowsMultipleSelectionDuringEditing = YES;
// 进入编辑模式
[self.tableView setEditing:YES animated:YES];

// 获得选中的所有行
self.tableView.indexPathsForSelectedRows;
时间: 2024-08-05 19:36:00

iOS-UI控件之UITableView(四)- cell数据刷新的相关文章

iOS UI控件7(UITableView)

1.表格(UITableView)与表格控制器(UITableViewController) UITableView是iOS开发中常见的UI控件,本质是一个列表(单列的表格).UITableView允许自由控制行的控件,包括在表格行中添加多个字控件.UITableView继承了UIScrollView,具有UIScrollView的功能,这个UIScrollView主要封装了UITableViewCell单元格控件,因此UITableView默认可以对单元格进行滚动.默认情况下,所有UITabl

IOS Ui控件 修改位置和尺寸,代码添加控件

所有的UI控件最终都继承自UIView,UI控件的公共属性都定义在UIView中, UIView的常见属性 UIView *superview; 获得自己的父控件对象 NSArray *subviews; 获得自己的所有子控件对象 NSInteger tag; 控件的ID(标识),父控件可以通过tag来找到对应的子控件 CGAffineTransform transform; 控件的形变属性(可以设置旋转角度.比例缩放.平移等属性) CGRect frame; 控件所在矩形框在父控件中的位置和尺

iOS UI控件没有显示时的调试技巧

1.遇到UI控件没有显示的问题,可以给这个控件设置背景色 假设这个控件是UIBUtton 如果背景色能显示,那问题就出在image和title值为空 如果背景色不能显示,重写控件的description方法,把控件的frame打印出来分析 2.以下是打印UIView的frame的分类 #import <UIKit/UIKit.h> @interface UIView (Log) @end 1 #import "UIView+Log.h" 2 3 @implementatio

iOS UI控件6

1.微调器(UIStepper) iOS5 新增UI,包含 +.-两个按钮,继承了UIControl 支持的属性: Value Minimum Maximum Current Step Behavior Autorepeat 按住 加号 不松手,数字会持续变化 Continuous 为YES时,用户交互会立即出发ValueChanged事件,NO 表示只有等用户交互结束才出发ValueChanged事件 Wrap 若为YES,value加到超过Maximum值时,会变成Min指. 设置自定义外观

ios UI控件的简单整理

把该文件复制到.m文件里就能够方便的查找 /** 匿名类目:可以声明方法和变量,属性为private(不允许在外部调用,且不能被继承 */ /** 发送数据的委托方,接收数据的时代理发(即代理的反向传值) 委托方第一步:声明协议 委托方第二步:声明代理指针 委托方第三步:操作完成,告诉代理(即调用代理的方法) 代理第一步:遵守协议 代理第二步:成为代理 代理第三步:实现协议方法 */ // %zd %zi 表示NSInteger // %g 表示数字去掉尾零 //代码块路径 /Users/ms/

蓝懿 iOS UI控件

今天上课讲了一堆控件,主要有UIImageview,UIview,UIButton,UILable,UITextFiled,UITextview,包括进度条,缓冲圈开关的用法,比较难多是UIButton的一些显示状态的应用,还有UITextFiled的UITextFiledDeleGate协议,在协议中调用一些时间节点来实现想要的结果: 今天上课到现在脑子里一直充斥着各种控件的方法和属性的应用,每个控件都有很多中属性和方法,可以通过按command键然后在控件名上右击进去查看,如果要点用方法可以

ios基础控件 之展示团购数据 UITableViewCell &lt;代理,xib封装view&gt;

1.主控制器: 1 // 2 // ViewController.m 3 // GroupPurchase 4 // 5 // Created by hellovoidworld on 14/12/3. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "GroupPurchase.h" 11 #im

UI控件之UITableView的协议方法

<UITableViewDataSource,UITableViewDelegate> //设置表视图的编辑状态 -(void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [_tableView setEditing:editing animated:animated]; } //设置编辑模式(插入.删除.选择),默认是删除 -(UITableV

创建自注册的Swift UI 控件

原文链接 : Swift Programming 101: Creating Self-Registering Swift UI Controls 原文作者 : Kevin McNeish 译文出自 : 开发技术前线 www.devtf.cn 译者 : kmyhy 校对者:LastDay 状态:完成 对于自定义控件来说,在不破坏原有的消息机制的前提下,如何响应事件通知?在本文中,我将演示一个通知代理类,通过一个简单的例子,我们用该类向已有的iOS UI控件中增加了自己的新功能:为Text Vie

IOS开发UI篇--常用UI控件的基本使用

一. UIButton概述: UIKit框架提供了非常多的UI控件,其中有些控件天天使用,比如UIButton.UILabel.UIImageView.UITableView等. UIButton,俗称“按钮”,通常点击某个控件后,会做出相应反应的都是按钮.按钮的功能较多,既能显示图片又能显示汉字还能随时调整图片的文字和位置,如下面两个图 团购和音乐播放器的app: 下面本文通过一个实例总结一下它们的基本使用. 二. 按钮的基本设置 按钮既可以通过mainstoryboard创建也可以通过代码创