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];/*//使用 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重用机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java数据结构习题_算法分析
- 下一篇: 导入第三方库报 unknown type