UI部分 — 10
2018-01-29 20:55
141 查看
- 数据刷新
(1)添加
- (IBAction)add {
//修改模型
XYWine *wine = [[XMGWinealloc]
init];
wine.image =@"newWine";
wine.money =@"55.5";
wine.name =@"女儿红";
//[self.wineArray addObject:wine];
[self.wineArrayinsertObject:wine
atIndex:0];
//刷新数据(全局刷新)
[self.tableViewreloadData];
}
(2)删除
- (IBAction)remove {
//修改模型
[self.wineArrayremoveObjectAtIndex:0];
[self.wineArrayremoveObjectAtIndex:0];
//刷新数据
[self.tableViewreloadData];
}
(3)更新
- (IBAction)update {
//修改模型
XYWine *wine =self.wineArray[0];
wine.money =@"100";
//刷新数据
[self.tableViewreloadData];
}
(4)局部刷新
- (IBAction)add {
//修改模型
XYWine *wine = [[XYWinealloc]
init];
wine.image =@"newWine";
wine.money =@"55.5";
wine.name =@"女儿红";
[self.wineArrayinsertObject:wine
atIndex:0];
//局部刷新
NSArray *indexPaths =@[
[NSIndexPathindexPathForRow:0inSection:0]
];
[self.tableViewinsertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationRight];
}
- (IBAction)remove {
//修改模型
[self.wineArrayremoveObjectAtIndex:0];
[self.wineArrayremoveObjectAtIndex:0];
//局部刷新
NSArray *indexPaths =@[
[NSIndexPathindexPathForRow:0inSection:0],
[NSIndexPathindexPathForRow:1inSection:0],
];
[self.tableViewdeleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationMiddle];
}
- (IBAction)update {
//修改模型
XYWine *wine =self.wineArray[0];
wine.money =@"100";
XYWine *wine2 =self.wineArray[1];
wine2.money =@“80”;
//局部刷新
NSArray *indexPaths =@[
[NSIndexPathindexPathForRow:0inSection:0],
[NSIndexPathindexPathForRow:1inSection:0]
];
[self.tableViewreloadRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationLeft];
}
- 左滑删除
(1)需要遵守协议<UITableViewDelegate>
//只要实现这个方法,就拥有左滑删除功能
//点击左滑出现的Delete按钮,就会调用这个方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath
*)indexPath
{
[self.wineArrayremoveObjectAtIndex:indexPath.row];
[self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];
}
(2)修改默认Delete按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath
{
return
@"删除";
}
(3)左滑出现自定义按钮
//点击左滑出现的自定义按钮,就会调用这个方法
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UITableViewRowAction *action = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"关注"handler:^(UITableViewRowAction
*_Nonnull action,NSIndexPath *
_Nonnull indexPath) {
//退出编辑模式(恢复原本的cell样式)
self.tableView.editing =NO;
}];
UITableViewRowAction *action1 = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDestructivetitle:@"删除"handler:^(UITableViewRowAction
*_Nonnull action,NSIndexPath *
_Nonnull indexPath) {
[self.wineArrayremoveObjectAtIndex:indexPath.row];
//删除
[self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}];
return@[action, action1];
}
(3)编辑模式
- (IBAction)remove {
//若当前不是编辑模式,进入编辑模式(出现一排删除圆点按钮),否则退出编辑模式
//self.tableView.editing = !self.tableView.isEditing;
[self.tableViewsetEditing:!self.tableView.isEditinganimated:YES];
}
- 批量删除
(1)- (void)viewDidLoad
{
[superviewDidLoad];
//告诉tableView在编辑模式下可以多选
self.tableView.allowsMultipleSelectionDuringEditing
=YES;
self.deletedButton.hidden
=YES;
}
- (IBAction)MultipleRemove {
//进入编辑模式
[self.tableViewsetEditing:!self.tableView.isEditinganimated:YES];
self.deletedButton.hidden
= !self.tableView.isEditing;
}
- (IBAction)remove {
//注意:千万不要一边遍历一边删除,因为每删除一个元素,其他元素的索引可能会发生变化
NSMutableArray *deletedWine = [NSMutableArrayarray];
for (NSIndexPath *indexPathin
self.tableView.indexPathsForSelectedRows) {
[deletedWine
addObject:self.wineArray[indexPath.row]];
}
//修改模型
[self.wineArrayremoveObjectsInArray:deletedWine];
//刷新表格
[self.tableViewdeleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRowswithRowAnimation:UITableViewRowAnimationAutomatic];
}
- 自定义批量删除
(1)XYWineCell.m
#import
"XYWineCell.h"
#import
"XYWine.h"
@interfaceXYWineCell ()
//打钩控件
@property (nonatomic,weak)
UIImageView *checkedImageView;
@end
@implementation XYWineCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [superinitWithStyle:style
reuseIdentifier:reuseIdentifier]) {
//添加打钩控件
UIImageView *checkedImageView = [[UIImageViewalloc]
init];
checkedImageView.hidden =YES;
checkedImageView.image = [UIImageimageNamed:@"check"];
[self.contentViewaddSubview:checkedImageView];
self.checkedImageView = checkedImageView;
}
return
self;
}
- (void)layoutSubviews
{
[superlayoutSubviews];
//设置打钩的位置和尺寸
CGFloat WH =24;
CGFloat X =
self.contentView.frame.size.width - WH -
10;
CGFloat Y = (self.contentView.frame.size.height
- WH) * 0.5;
self.checkedImageView.frame =CGRectMake(X, Y, WH, WH);
//调整textLabel的宽度
CGRect frame =
self.textLabel.frame;
frame.size.width =self.contentView.frame.size.width
- WH -20 -
self.textLabel.frame.origin.x;
self.textLabel.frame = frame;
}
- (void)setWine:(XYWine *)wine
{
_wine = wine;
self.textLabel.text = wine.name;
self.imageView.image
= [UIImageimageNamed:wine.image];
self.detailTextLabel.text
= [NSStringstringWithFormat:@"¥%@",wine.money];
//根据模型的checked属性确定打钩控件显示还是隐藏
if (wine.isCheched) {
self.checkedImageView.hidden
=NO;
}
else {
self.checkedImageView.hidden =YES;
}
}
@end
XYWine.h
#import
<Foundation/Foundation.h>
@interface XYWine :NSObject
@property (nonatomic ,copy)NSString *image;
@property (nonatomic ,copy)NSString *money;
@property (nonatomic ,copy)NSString *name;
//记录打钩控件的状态
@property (nonatomic,assign,
getter=isCheched)BOOL checked;
@end
ViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//修改模型
XYWine *wine =self.wineArray[indexPath.row];
if (wine.isCheched) { //之前是打钩的,取消打钩
wine.checked =NO;
[self.seletedIndexPathremoveObject:indexPath];
}
else { //之前不是打钩的,现在打钩
wine.checked =YES;
[self.seletedIndexPathaddObject:indexPath];
}
//刷新表格
[self.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}
(1)添加
- (IBAction)add {
//修改模型
XYWine *wine = [[XMGWinealloc]
init];
wine.image =@"newWine";
wine.money =@"55.5";
wine.name =@"女儿红";
//[self.wineArray addObject:wine];
[self.wineArrayinsertObject:wine
atIndex:0];
//刷新数据(全局刷新)
[self.tableViewreloadData];
}
(2)删除
- (IBAction)remove {
//修改模型
[self.wineArrayremoveObjectAtIndex:0];
[self.wineArrayremoveObjectAtIndex:0];
//刷新数据
[self.tableViewreloadData];
}
(3)更新
- (IBAction)update {
//修改模型
XYWine *wine =self.wineArray[0];
wine.money =@"100";
//刷新数据
[self.tableViewreloadData];
}
(4)局部刷新
- (IBAction)add {
//修改模型
XYWine *wine = [[XYWinealloc]
init];
wine.image =@"newWine";
wine.money =@"55.5";
wine.name =@"女儿红";
[self.wineArrayinsertObject:wine
atIndex:0];
//局部刷新
NSArray *indexPaths =@[
[NSIndexPathindexPathForRow:0inSection:0]
];
[self.tableViewinsertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationRight];
}
- (IBAction)remove {
//修改模型
[self.wineArrayremoveObjectAtIndex:0];
[self.wineArrayremoveObjectAtIndex:0];
//局部刷新
NSArray *indexPaths =@[
[NSIndexPathindexPathForRow:0inSection:0],
[NSIndexPathindexPathForRow:1inSection:0],
];
[self.tableViewdeleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationMiddle];
}
- (IBAction)update {
//修改模型
XYWine *wine =self.wineArray[0];
wine.money =@"100";
XYWine *wine2 =self.wineArray[1];
wine2.money =@“80”;
//局部刷新
NSArray *indexPaths =@[
[NSIndexPathindexPathForRow:0inSection:0],
[NSIndexPathindexPathForRow:1inSection:0]
];
[self.tableViewreloadRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationLeft];
}
- 左滑删除
(1)需要遵守协议<UITableViewDelegate>
//只要实现这个方法,就拥有左滑删除功能
//点击左滑出现的Delete按钮,就会调用这个方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath
*)indexPath
{
[self.wineArrayremoveObjectAtIndex:indexPath.row];
[self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];
}
(2)修改默认Delete按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath
{
return
@"删除";
}
(3)左滑出现自定义按钮
//点击左滑出现的自定义按钮,就会调用这个方法
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UITableViewRowAction *action = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"关注"handler:^(UITableViewRowAction
*_Nonnull action,NSIndexPath *
_Nonnull indexPath) {
//退出编辑模式(恢复原本的cell样式)
self.tableView.editing =NO;
}];
UITableViewRowAction *action1 = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDestructivetitle:@"删除"handler:^(UITableViewRowAction
*_Nonnull action,NSIndexPath *
_Nonnull indexPath) {
[self.wineArrayremoveObjectAtIndex:indexPath.row];
//删除
[self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}];
return@[action, action1];
}
(3)编辑模式
- (IBAction)remove {
//若当前不是编辑模式,进入编辑模式(出现一排删除圆点按钮),否则退出编辑模式
//self.tableView.editing = !self.tableView.isEditing;
[self.tableViewsetEditing:!self.tableView.isEditinganimated:YES];
}
- 批量删除
(1)- (void)viewDidLoad
{
[superviewDidLoad];
//告诉tableView在编辑模式下可以多选
self.tableView.allowsMultipleSelectionDuringEditing
=YES;
self.deletedButton.hidden
=YES;
}
- (IBAction)MultipleRemove {
//进入编辑模式
[self.tableViewsetEditing:!self.tableView.isEditinganimated:YES];
self.deletedButton.hidden
= !self.tableView.isEditing;
}
- (IBAction)remove {
//注意:千万不要一边遍历一边删除,因为每删除一个元素,其他元素的索引可能会发生变化
NSMutableArray *deletedWine = [NSMutableArrayarray];
for (NSIndexPath *indexPathin
self.tableView.indexPathsForSelectedRows) {
[deletedWine
addObject:self.wineArray[indexPath.row]];
}
//修改模型
[self.wineArrayremoveObjectsInArray:deletedWine];
//刷新表格
[self.tableViewdeleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRowswithRowAnimation:UITableViewRowAnimationAutomatic];
}
- 自定义批量删除
(1)XYWineCell.m
#import
"XYWineCell.h"
#import
"XYWine.h"
@interfaceXYWineCell ()
//打钩控件
@property (nonatomic,weak)
UIImageView *checkedImageView;
@end
@implementation XYWineCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [superinitWithStyle:style
reuseIdentifier:reuseIdentifier]) {
//添加打钩控件
UIImageView *checkedImageView = [[UIImageViewalloc]
init];
checkedImageView.hidden =YES;
checkedImageView.image = [UIImageimageNamed:@"check"];
[self.contentViewaddSubview:checkedImageView];
self.checkedImageView = checkedImageView;
}
return
self;
}
- (void)layoutSubviews
{
[superlayoutSubviews];
//设置打钩的位置和尺寸
CGFloat WH =24;
CGFloat X =
self.contentView.frame.size.width - WH -
10;
CGFloat Y = (self.contentView.frame.size.height
- WH) * 0.5;
self.checkedImageView.frame =CGRectMake(X, Y, WH, WH);
//调整textLabel的宽度
CGRect frame =
self.textLabel.frame;
frame.size.width =self.contentView.frame.size.width
- WH -20 -
self.textLabel.frame.origin.x;
self.textLabel.frame = frame;
}
- (void)setWine:(XYWine *)wine
{
_wine = wine;
self.textLabel.text = wine.name;
self.imageView.image
= [UIImageimageNamed:wine.image];
self.detailTextLabel.text
= [NSStringstringWithFormat:@"¥%@",wine.money];
//根据模型的checked属性确定打钩控件显示还是隐藏
if (wine.isCheched) {
self.checkedImageView.hidden
=NO;
}
else {
self.checkedImageView.hidden =YES;
}
}
@end
XYWine.h
#import
<Foundation/Foundation.h>
@interface XYWine :NSObject
@property (nonatomic ,copy)NSString *image;
@property (nonatomic ,copy)NSString *money;
@property (nonatomic ,copy)NSString *name;
//记录打钩控件的状态
@property (nonatomic,assign,
getter=isCheched)BOOL checked;
@end
ViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//修改模型
XYWine *wine =self.wineArray[indexPath.row];
if (wine.isCheched) { //之前是打钩的,取消打钩
wine.checked =NO;
[self.seletedIndexPathremoveObject:indexPath];
}
else { //之前不是打钩的,现在打钩
wine.checked =YES;
[self.seletedIndexPathaddObject:indexPath];
}
//刷新表格
[self.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}
相关文章推荐
- iOS开发视频教程 UI部分:10、开发者计划和作业
- MS CRM 2011的自定义和开发(10)——CRM web服务介绍(第二部分)——IOrganizationService(一)
- ios学习记录 day34 UI10+ UITableView编辑
- iOS UI10_cell自适应高度
- AS3 Starling塔防教程——第八部分——UI菜单组件
- ios ui部分各个控件使用
- Atititi ui之道 attilax著 v3 s11.docx 1. 概览 2 1.1. 软件设计可分为两个部分:编码设计与UI设计 2 2. 用户界面设计的三大原则是:置界面于用户的控制之下;
- 第三部分:Android 应用程序接口指南---第二节:UI---第二章 输入控件
- 第三部分:Android 应用程序接口指南---第二节:UI---第十章 拖放
- 一步一步学Streams:第二部分(10)实践之准备工作(2)初始化环境
- iOS部分-UI基础控件 - 01天 入门 第07课 私有扩展&IBAction
- 体验魅力 Cognos BI 10 系列,第 1 部分: 第一次安装
- iOS 中UI部分一些类的继承关系
- css笔记10:多个id选择器/类选择器包含相同部分问题的探讨
- iOS小知识点(UI部分)
- iOS开发视频教程 UI部分:3、Hello World
- IOS开发中UI部分UIWebView简单使用<代码演示>
- jquery UI API-滑块部分(Slider widget)
- Android doc |Getting Started|部分 --Building a Flexible UI(部分翻译)
- 重读《Java编程思想(第四版)》(10-17章、18章部分)