tableView编辑、tableView移动、UITableViewController

tableView的编辑:cell的添加、删除。
使⽤场景:
删除⼀个下载好的视频,删除联系⼈;
插⼊⼀条新的聊天记录等

1、让tableView处于编辑状态

2、指定tableView哪些⾏可以编辑

3、指定tableView编辑的样式(添加、删除)

4、编辑完成(先操作数据源,再修改UI)

移动的步骤

1、让tableView处于编辑状态

2、指定tableView哪些⾏可以移动

3、移动完成

监测移动过程,实现限制跨区移动

UITableViewController

UITableViewController继承⾃UIViewController,⾃带⼀个tableView
self.view不是UIView⽽是UITableView
datasource和delegate默认都是self(UITableViewController)
开发中只需要建⽴UITableViewController⼦类

⽆论编辑还是移动,都先让tableView进⼊编辑状态。
编辑结束或者移动结束,要先修改数组或字典中的数据,在更改UI。
UITableViewController是封装好了各种delegate和datasource,能提⾼我们开发速度。

代码:

#import "AppDelegate.h"
#import "RootController.h"
#import "NewTableViewController.h"@interface AppDelegate ()@end@implementation AppDelegate-(void)dealloc{[self.window release];[super dealloc];
}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// Override point for customization after application launch.self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];/* */RootController * RootVC = [[RootController alloc]init];UINavigationController * navl = [[UINavigationController alloc]initWithRootViewController:RootVC];self.window.rootViewController = navl;[RootVC release];[navl release];/*//使用 uitableViewcontrollerNewTableViewController * RootVC = [[NewTableViewController alloc]init];UINavigationController * navl = [[UINavigationController alloc]initWithRootViewController:RootVC];self.window.rootViewController = navl;[RootVC release];[navl release];*/return YES;
}

View Code AppDelegate.m

#import <UIKit/UIKit.h>
#import "Contacts.h"
#import "UIImage+Scale.h"
#import "CustomCell.h"@interface RootController : UIViewController@end

View Code RootController.h

//
//  RootController.m

#import "RootController.h"
#import "DetailViewController.h"@interface RootController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableDictionary * dataDic;//存储所有联系人
@property(nonatomic,retain)NSMutableArray * sortedKeys;//存储排好序的 key
@property(nonatomic,retain)Contacts * per1;
@end
/*一个工程的基本框架的规范组成Appdelegate //这里存放一些工程的代理事件Resource // 存放工程的公共资源 图片 音频General // 存放共有的类,可以重复使用的共有的类Macro // 存放一些宏定义Vender //存放第三方类Section {模块一{ Controller Model  View }模块二{ Controller Model  View }模块三{ Controller Model  View }...}
*/
@implementation RootController
/*tableView 的编辑1.添加编辑按钮2.重写 setEditing:(BOOL)editing animated:(BOOL)animated 方法3.设置 tableView 的可编辑状态4.设置 tableView 的编辑样式 也可以设置哪些行可以被编辑 (Delegate)5.提交编辑状态 对数据以及界面进行处理 (真正的数据是放在集合或或数组,者字典里)*/- (void)viewDidLoad {[super viewDidLoad];UITableView * tableView =[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];tableView.separatorColor = [UIColor grayColor];tableView.dataSource = self;//设置数据源tableView.delegate = self;//设置代理tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);self.view = tableView;//设置 tableView 为根视图
    [tableView release];[self coustomNavBar];//添加系统自带的编辑按钮//从本地读取数据
    [self readDAtaFromLocal];
}
#pragma mark ---------读取本地数据
-(void)readDAtaFromLocal{NSString  * filePath = [[NSBundle mainBundle]pathForResource:@"contacts" ofType:@"plist"];self.dataDic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];NSDictionary * dic = [NSMutableDictionary dictionaryWithDictionary:self.dataDic];//拷贝出来一份给不可变字典  对不可变字典遍历NSDictionary * dict = [NSDictionary dictionaryWithDictionary:dic];//获取拍好序的 keyNSArray * sorted = [[dict allKeys]sortedArrayUsingSelector:@selector(compare:)];self.sortedKeys = [NSMutableArray arrayWithArray:sorted];//不能一边遍历结合一边操作//外层字典遍历
//    for (NSString * key in dict) { 得到的是无序的for (NSString * key in _sortedKeys) {NSMutableArray * contactArr = [NSMutableArray array];NSArray * group = [dict objectForKey:key];//内层遍历获取每一个分组 将 dic 的信息封装到 Contacts 对象里面for (NSDictionary * dic in group) {Contacts *per = [[Contacts alloc]initWithDic:dic];//将联系人村放到数组中
            [contactArr addObject: per];}//重新把原来的大字典赋拍好序的信息
        [self.dataDic setValue:contactArr forKey:key];}
}
#pragma mark0 --------必须实现的两个方法
//设置分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return [self.dataDic[_sortedKeys[section]] count];//不同的section就是不同的key,代表了不同的组, section的值即为key排好序后的 数组的下标NSArray *everyKeyForGroup = [self.dataDic valueForKey: self.sortedKeys[section]];return everyKeyForGroup.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString * identifier = @"cell";CustomCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];//如果没有获取成功,就新建 cellif (!cell) {cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier]autorelease];}//获取分组的 keyNSString * key =  [_sortedKeys objectAtIndex:indexPath.section];//获取对应的分组NSArray * group = [self.dataDic objectForKey:key];//获取联系人对象Contacts * contact = [group objectAtIndex:indexPath.row];//为 cell 赋新值self.per1 = contact;cell.nameLabel.text = contact.name;cell.contentLabel.text =contact.phoneNum;cell.photoView.image = [[UIImage imageNamed:contact.photo]scaleToSize:CGSizeMake(50, 50)];//使用到了图片的方法的分类
    [cell.callBtn addTarget:self action:@selector(handleCallBtn:) forControlEvents:UIControlEventTouchUpInside];//点击呼叫return cell;
}
//打电话就条状下一页(这里的功能还没有实现?????????????)
-(void)handleCallBtn:(UIButton *)sender{DetailViewController * detalVC = [[DetailViewController alloc]init];detalVC.name = self.per1.name;detalVC.phonenum = self.per1.phoneNum;detalVC.image = [UIImage imageNamed:self.per1.photo];[self.navigationController pushViewController:detalVC animated:YES];[detalVC release];
}
-(void)viewWillDisappear:(BOOL)animated{}
//设置分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return _sortedKeys.count;
}
//设置分区标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{return self.sortedKeys[section];
}
//指定行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 60;
}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}
#pragma mark1 ------------添加系统自带的编辑按钮
-(void)coustomNavBar{//添加系统自带的编辑按钮 (done 不可编辑 edit 可以编辑)self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark2 ------------ 重写 setEditing:(BOOL)editing animated:(BOOL)animated 方法
//重写方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{[super setEditing:editing animated:animated];//editing Edit:YES 可编辑的   Done : NO 不可编辑//设置 tableview 的编辑状态 目的就是让 tableView 处于编辑状态[(UITableView *)self.view setEditing:editing animated:YES];
}
#pragma mark3 -----------提交编辑状态 对数据以及界面进行处理 (真正的数据是放在集合或或数组,者字典里)
//设置哪些行可以被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{if (indexPath.row == 1) {//某行能否被修改return YES;}return YES;
}#pragma mark4 ------------设置 tableView 的编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{if (indexPath.section == 0) {//设置第一分组可以添加 一些数据return UITableViewCellEditingStyleInsert;//插入样式
    }return UITableViewCellEditingStyleDelete;//删除样式
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0){return @"点我删除";
}//提交编辑状态 提交编辑状态的时刻 被触发
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//获取到编辑的这一行所在的位置 (key)NSString * key  = [self.sortedKeys objectAtIndex:indexPath.section];//获取到联系人的数组NSMutableArray * group = [_dataDic objectForKey:key];//获取到对应的联系人对象Contacts * contact = [group objectAtIndex:indexPath.row];
//    Contacts * contact = group[indexPath.row];//也可这样写//判断编辑状态if (editingStyle == UITableViewCellEditingStyleDelete) {
//        if (indexPath.row == 2) {//0
//            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        }//删除操作//需要判断是否需要删除对应的分区,当数组的联系人只有一个的时候,这时候需要把该联系人对应的分区也要被删除if (group.count == 1) {//删除整个分区//1.数据源删除 删除对应分组的信息[_dataDic removeObjectForKey:key];//删除对应的分组的数据[_sortedKeys removeObject:key];//删除对应的 key//2.界面上删除 修改界面  删除所在分区的界面
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationBottom];}else{//删除该行//1.数据源[group removeObject:contact];//删除联系人//2.界面
#warning  mark @[indexPath]   什么意思?[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}}//添加编辑状态else{//添加操作//1.数据源操作NSDictionary * dic = @{@"name":@"白白",@"gender":@"男",@"phoneNum":@"12345625602",@"photo":@"uuuuuu"};//将字典封装成联系人对象Contacts * newPer = [[Contacts alloc]initWithDic:dic];//将联系人添加到对应的分区的联系人数组里[group insertObject:newPer atIndex:indexPath.row];//添加一个联系人//2.界面操作[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];//界面上添加一个联系人
        [newPer release];}
}// PM
#pragma mark5 ------------设置 tableView 的cell 的移动
//移动(设置某些行的 cell 可以移动)(先打一个 BOOl 寻找方法)
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{return YES;
}
//提交移动的操作 (先打 void 再寻找方法)
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{//该方法 sourceIndexPath 是原来的区域 destinationIndexPath 是移动后的区域//做移动操作的时候,界面上已经发生了改变.所以我们只需要处理数据上的//获取分组的数组//获取外层大字典的 keyNSString * key = [self.sortedKeys objectAtIndex:sourceIndexPath.section];NSMutableArray * group =  [_dataDic objectForKey:key];//这里写 retain 的原因就是让其引用计数器加1,保持所有权Contacts * per = [[group objectAtIndex:sourceIndexPath.row]retain];//让引用计数加1,保证对象的存在//从数组中把对应的 per 对象从数组中删除
    [group removeObjectAtIndex:sourceIndexPath.row];//然后再把对应的对象移动到目的位置
    [group insertObject:per atIndex:destinationIndexPath.row];[per release];
}
//限定 cell 的移动界限 ----禁止跨区移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{//tableView  当前操作的 tableview//sourceIndexPath 移动之前的下标索引值 就是移动之前的 cell 的位置//proposedDestinationIndexPath 移动之后所得到的目的位置//如果移动之前 和 移动之后的所在分区是同一个分区,则支持移动if ( sourceIndexPath.section == proposedDestinationIndexPath.section) {return proposedDestinationIndexPath;//移动的位置}else{return sourceIndexPath;//原来位置
    }}
@end

View Code RootController.m

#import <UIKit/UIKit.h>@interface NewTableViewController : UITableViewController@end

View Code NewTableViewController.h

//
//  NewTableViewController.m

#import "NewTableViewController.h"@interface NewTableViewController ()@end@implementation NewTableViewController
/*UITableViewController 和 UIViewController 的区别1.前者的根视图是 tableView  后者是 UIView2.如果用 UIView 的话 需要再设置 dataSource ,前者不用再设置 dataSource 和 delegate ,同时也不用再服从协议,因为自身已经服从了, 而后者我们需要指定他的 dataSource 和 delegate3.前者不用重写 setEdting:Animation : 方法控制 tabelview,后者需要指定4.前者已经自动的帮我们生成了对应的 dataSource 的最基本最常用的协议的方法(需要使用,就注开就可以了),后者需要自己去手动添加相应的协议方法*/
- (void)viewDidLoad {[super viewDidLoad];//     Uncomment the following line to preserve selection between presentations.
//     self.clearsSelectionOnViewWillAppear = NO;//     Uncomment the following line to display an Edit button in the navigation bar for this view controller.self.navigationItem.rightBarButtonItem = self.editButtonItem;
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];if ([self isViewLoaded] && !self.view.window) {self.view = nil;}
}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.// Return the number of sections.return 4;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.// Return the number of rows in the section.return 2;
}/*UIActionSheetUIAlertView调用系统的相册,查询相册UIDatePickerUIPickerView绘图 DrawRectUITextViewUIToolBar*/- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {//设置重用标志符static NSString * identifier = @"cell";//根据重用的标志符在 重用列表中取 cellUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//如果没有获取重用的 cell ,则新建if (!cell) {cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];}// Configure the cell...cell.textLabel.text = @"时间广场";return cell;
}/**/
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {// Return NO if you do not want the specified item to be editable.return YES;
}/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {// Delete the row from the data source[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];} else if (editingStyle == UITableViewCellEditingStyleInsert) {// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view}
}
*//*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*//*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {// Return NO if you do not want the item to be re-orderable.return YES;
}
*//*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end

View Code NewTableViewController.m

#import <Foundation/Foundation.h>@interface Contacts : NSObject@property(nonatomic,copy)NSString * name;
@property(nonatomic,copy)NSString * gender;
@property(nonatomic,copy)NSString * phoneNum;
@property(nonatomic,copy)NSString * photo;-(id)initWithDic:(NSDictionary *)dic;@end

View Code Contacts.h

#import "Contacts.h"@implementation Contacts-(id)initWithDic:(NSDictionary *)dic{self  = [super init];if (self) {[self setValuesForKeysWithDictionary:dic];}return self;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {NSLog(@"key值不存在(⊙o⊙)哦");
}@end

View Code Contacts.m

#import <UIKit/UIKit.h>@interface CustomCell : UITableViewCell
@property (nonatomic , retain) UIImageView *photoView;
@property (nonatomic , retain) UILabel *nameLabel;
@property (nonatomic , retain) UILabel *contentLabel;
@property (nonatomic , retain) UIButton *callBtn;@end

View Code CustomCell.h

//
//  CustomCell.m

#import "CustomCell.h"@implementation CustomCell-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {[self customSubViews];//自定义 cell 控件
    }return self;
}-(void)customSubViews{//photoViewself.photoView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 8, self.frame.size.width / 4 - 20, self.frame.size.height)];
//    _photoView.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:_photoView];_photoView.layer.cornerRadius = 20;_photoView.layer.masksToBounds = YES;//当绘制底层的边界的时候,本控件也和边界一起绘制
    [_photoView release];//nameLableself.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 4 , 12, self.frame.size.width / 3 - 20, self.frame.size.height -20)];_nameLabel.textAlignment = UITextAlignmentCenter;
//    _nameLabel.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:_nameLabel];[_nameLabel release];//contenlableself.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nameLabel.frame.origin.x + self.frame.size.width / 3 - 20, 12, self.frame.size.width / 3 , self.frame.size.height -20)];
//    _contentLabel.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:_contentLabel];[_contentLabel release];//callBtnself.callBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];_callBtn.backgroundColor = [UIColor redColor];_callBtn.alpha = 0.4;_callBtn.frame = CGRectMake(_nameLabel.frame.origin.x + self.frame.size.width / 3 - 8 + self.frame.size.width / 3  + 5, 12, self.frame.size.width / 6 , self.frame.size.height -20);
//    [_callBtn addTarget:self action:@selector(handleCallBtn:) forControlEvents:UIControlEventTouchUpInside];[_callBtn setTitle:@"呼叫" forState:UIControlStateNormal];[self.contentView addSubview:_callBtn];
}@end

View Code CustomCell.m

#import <UIKit/UIKit.h>@interface DetailViewController : UITableViewController
@property(nonatomic,retain)NSString * name;
@property(nonatomic,retain)NSString * phonenum;
@property(nonatomic,retain)UIImage * image;
@end

View Code DetailViewController.h

//
//  DetailViewController.m

#import "DetailViewController.h"@interface DetailViewController ()@end@implementation DetailViewController- (void)viewDidLoad {[super viewDidLoad];[self setUpDetail];[self commensetting];}-(void)commensetting{
//    self.navigationItem.title = @"XXX详细信息";
}
-(void)setUpDetail{UIView * backView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];self.view  = backView;backView.backgroundColor = [UIColor orangeColor];UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)];[imageView setImage:self.image];[self.view addSubview:imageView];imageView.backgroundColor = [UIColor blackColor];UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(200, 100, 60, 30)];lable1.textAlignment = UITextAlignmentLeft;lable1.text = self.name;[self.view addSubview:lable1];[lable1 release];UILabel *lable2 = [[UILabel alloc]initWithFrame:CGRectMake(200, 150, 60, 30)];lable2.textAlignment = UITextAlignmentLeft;lable2.text = self.phonenum;[self.view addSubview:lable2];[lable2 release];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];if ([self isViewLoaded] && !self.view.window) {self.view = nil;}
}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.// Return the number of sections.return 0;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.// Return the number of rows in the section.return 0;
}/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];// Configure the cell...return cell;
}
*//*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {// Return NO if you do not want the specified item to be editable.return YES;
}
*//*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {// Delete the row from the data source[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];} else if (editingStyle == UITableViewCellEditingStyleInsert) {// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view}
}
*//*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*//*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {// Return NO if you do not want the item to be re-orderable.return YES;
}
*//*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end

View Code DetailViewController.m

#import <UIKit/UIKit.h>@interface UIImage (Scale)
//获取指定大小的图片
-(UIImage *)scaleToSize:(CGSize)size;
@end

View Code UIImage+Scale.h

#import "UIImage+Scale.h"@implementation UIImage (Scale)
-(UIImage *)scaleToSize:(CGSize)size{//绘制图片//创建一个 bitmap 的上下文,并指定为当前使用的 context
    UIGraphicsBeginImageContext(size);//根据外界传入的大小绘制改变大小后的图片[self drawInRect:CGRectMake(0, 0, size.width, size.height)];//从当前的 context 获取改变大小后的图片UIImage * scaleImage = UIGraphicsGetImageFromCurrentImageContext();//使我们当前的 context 从栈顶出栈
    UIGraphicsEndImageContext();//返回改变大小后的图片return scaleImage;
}
@end

View Code UIImage+Scale.m

//为UItableView 的 cell 里添加一组相同规格的图片的时候用到

#import <UIKit/UIKit.h>@interface UIImage (Scale)
//获取指定大小的图片
-(UIImage *)scaleToSize:(CGSize)size;
@end

View Code UIImage+Scale.h

#import "UIImage+Scale.h"@implementation UIImage (Scale)
-(UIImage *)scaleToSize:(CGSize)size{//绘制图片//创建一个 bitmap 的上下文,并指定为当前使用的 context
    UIGraphicsBeginImageContext(size);//根据外界传入的大小绘制改变大小后的图片[self drawInRect:CGRectMake(0, 0, size.width, size.height)];//从当前的 context 获取改变大小后的图片UIImage * scaleImage = UIGraphicsGetImageFromCurrentImageContext();//使我们当前的 context 从栈顶出栈
    UIGraphicsEndImageContext();//返回改变大小后的图片return scaleImage;
}
@end

View Code UIImage+Scale.m

转载于:https://www.cnblogs.com/benpaobadaniu/p/4799896.html

UI:UITableView 编辑、cell重用机制相关推荐

  1. UITableViewCell中cell重用机制导致内容重复的方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的UITableViewCell,可以让UITableViewCell响应一些点击 ...

  2. iOS cell重用机制导致数据重叠显示解决方法

    当页面拉动需要显示新数据的时候,把最后一个cell进行删除 就有可以自定义cell 此方案即可避免重复显示. <span style="font-family:Microsoft Ya ...

  3. UI基础(四)之tableView (cell重用、原型cell、静态cell)/xib注意事项

    ---恢复内容开始--- 1.Cell的重用机制: 如下图所示:我们在写tableview的数据源方法的时候,在第三个方法中通常会碰到定义重用cell的三步骤 #pragma mark -- 数据源方 ...

  4. UI一揽子计划 9 (UITableView 、UITableView 、重用机制)

    一. UITableView UITableView继承自UIScrollView,所以可以滚动 表视图的每⼀一条数据都是显示在UITableViewCell对象中 表视图可以分区显⽰示数据,每个分区 ...

  5. UI一揽子计划 10 (UITableView 中cell 的编辑, 增加, 删除、UITableViewController 、)

    一.tableView的编辑 tableView 编辑的步骤:    *  1. 让tableView成为可编辑状态       编辑按钮触发方法    *  -1. 激活编辑状态 - (void)r ...

  6. UITableView 重用机制

    iphone重用机制是苹果为了实现大量数据显示而采用的一种节省内存的机制,但是在实际使用过程中,会有以下问题: 1.使用addSubView在每项上添加视图的时候会有重叠的现象.例如,UITableV ...

  7. iOS 和 Android:UITableView与RecycleView的重用机制比较

    引言:iOS和Android各有自己的列表组件.众所周知,列表组件一直都是移动端各个端中,组件重用.内存优化的重点.今天就来分析下iOS和Android各自的重用机制. Android:Recycle ...

  8. iOS开发-自己定义重用机制给ScrollerView加入子视图

    iOS开发-自己定义重用机制给ScrollerView加入子视图 事实上这个问题我非常早就想过,仅仅是没有通过去写程序实现,昨天有人提起,我就巧了一下 不知道大家打印郭tableview:cellfo ...

  9. iOS开发之--TableViewCell重用机制避免重复显示问题

    常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复 // 这样配置的话超过页面显示的内容会重复出现 - (UITableViewCell *)tableView:(U ...

最新文章

  1. Docker 公司是如何做社区的?
  2. 为什么很努力进步却不明显
  3. 学 Win32 汇编[17]: 关于压栈(PUSH)与出栈(POP) 之一
  4. 我的世界java版幻翼_我的世界:熬夜3天能见到“幻翼”?你错了,还要满足这7个条件!...
  5. python docker自动化_自动化 – 自动创建docker容器并启动python脚本
  6. 管理和维护RHCS集群
  7. 算法提高 质因数2(java)
  8. 微软将不再把 .NET Framework API 移植到 .NET Core 3.0
  9. MAC系统下 win7虚拟机上网应该怎么设置啊
  10. psenet的eval_ctw1500.py解析
  11. Excel根据公式生成插入语句
  12. c语言链表的数据结构,c语言实现通用数据结构(一):通用链表
  13. SumatraPDF安装包
  14. 【资料分享】《建筑照明设计标准》(GB50034-2013)
  15. Jensen不等式及其扩展
  16. Shell脚本介绍(资源)
  17. uniapp自建数字键盘
  18. maven创建web项目
  19. 光标突然由竖线变成小黑块怎么解决?
  20. 使用 ATS605LSG 的电动机驱动的磁体编码器设计

热门文章

  1. 【C++】 为什么C++空类占一个字节
  2. 【Linux系统编程】浅谈进程地址空间与虚拟存储空间
  3. 2020年日历电子版(打印版)_2020年第11期印花世界电子版/手机版,欢迎在线免费阅读!...
  4. 贝叶斯告诉你,投掷硬币概率可以是90%
  5. voip 音频采集时间_数字音频基础------从PCM说起
  6. Redis设计与实现RDB持久化
  7. 记录 之 tf.placeholder() 函数的意义及用法
  8. 数组赋值给vector和list,顺便说明int和size_t的区别
  9. mysql更新视图的时候有时候可以不满足视图条件的值也能更新成功
  10. 浅析C++中的this指针 通过空指针(NULL)可以正确调用一些类的成员函数?