UI:UITableView 编辑、cell重用机制

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];
    
    /*
    //使用 uitableViewcontroller
    NewTableViewController * 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];
    //获取拍好序的 key
    NSArray * 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];
    //如果没有获取成功,就新建 cell
    if (!cell) {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier]autorelease];
    }
    //获取分组的 key
    NSString * 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 是移动后的区域
    
    //做移动操作的时候,界面上已经发生了改变.所以我们只需要处理数据上的
    //获取分组的数组
    //获取外层大字典的 key
    NSString * 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  后者是 UIView
 2.如果用 UIView 的话 需要再设置 dataSource ,前者不用再设置 dataSource 和 delegate ,同时也不用再服从协议,因为自身已经服从了, 而后者我们需要指定他的 dataSource 和 delegate
 3.前者不用重写 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;
}

/*
 UIActionSheet
 UIAlertView
 调用系统的相册,查询相册
 UIDatePicker
 UIPickerView
 绘图 DrawRect
 UITextView
 UIToolBar
 */

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //设置重用标志符
    static NSString * identifier = @"cell";
    //根据重用的标志符在 重用列表中取 cell
    UITableViewCell *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{
    //photoView
    self.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];
    //nameLable
    self.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];
    
    //contenlable
    self.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];
    
    //callBtn
    self.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