日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

UI:UITableView 编辑、cell重用机制

發(fā)布時(shí)間:2024/4/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UI:UITableView 编辑、cell重用机制 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

tableView編輯、tableView移動(dòng)、UITableViewController

?

tableView的編輯:cell的添加、刪除。
使?場(chǎng)景:
刪除?個(gè)下載好的視頻,刪除聯(lián)系?;
插??條新的聊天記錄等

1、讓tableView處于編輯狀態(tài) ?

2、指定tableView哪些?可以編輯

3、指定tableView編輯的樣式(添加、刪除)

4、編輯完成(先操作數(shù)據(jù)源,再修改UI)

?

移動(dòng)的步驟

1、讓tableView處于編輯狀態(tài)

2、指定tableView哪些?可以移動(dòng)

3、移動(dòng)完成

監(jiān)測(cè)移動(dòng)過程,實(shí)現(xiàn)限制跨區(qū)移動(dòng)

?

UITableViewController

UITableViewController繼承?UIViewController,?帶?個(gè)tableView
self.view不是UIView?是UITableView
datasource和delegate默認(rèn)都是self(UITableViewController)
開發(fā)中只需要建?UITableViewController?類

?論編輯還是移動(dòng),都先讓tableView進(jìn)?編輯狀態(tài)。
編輯結(jié)束或者移動(dòng)結(jié)束,要先修改數(shù)組或字典中的數(shù)據(jù),在更改UI。
UITableViewController是封裝好了各種delegate和datasource,能提?我們開發(fā)速度。

?

代碼:

#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;//存儲(chǔ)所有聯(lián)系人 @property(nonatomic,retain)NSMutableArray * sortedKeys;//存儲(chǔ)排好序的 key @property(nonatomic,retain)Contacts * per1; @end /*一個(gè)工程的基本框架的規(guī)范組成Appdelegate //這里存放一些工程的代理事件Resource // 存放工程的公共資源 圖片 音頻General // 存放共有的類,可以重復(fù)使用的共有的類Macro // 存放一些宏定義Vender //存放第三方類Section {模塊一{ Controller Model View }模塊二{ Controller Model View }模塊三{ Controller Model View }...} */ @implementation RootController /*tableView 的編輯1.添加編輯按鈕2.重寫 setEditing:(BOOL)editing animated:(BOOL)animated 方法3.設(shè)置 tableView 的可編輯狀態(tài)4.設(shè)置 tableView 的編輯樣式 也可以設(shè)置哪些行可以被編輯 (Delegate)5.提交編輯狀態(tài) 對(duì)數(shù)據(jù)以及界面進(jìn)行處理 (真正的數(shù)據(jù)是放在集合或或數(shù)組,者字典里)*/- (void)viewDidLoad {[super viewDidLoad];UITableView * tableView =[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];tableView.separatorColor = [UIColor grayColor];tableView.dataSource = self;//設(shè)置數(shù)據(jù)源tableView.delegate = self;//設(shè)置代理tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);self.view = tableView;//設(shè)置 tableView 為根視圖 [tableView release];[self coustomNavBar];//添加系統(tǒng)自帶的編輯按鈕//從本地讀取數(shù)據(jù) [self readDAtaFromLocal]; } #pragma mark ---------讀取本地?cái)?shù)據(jù) -(void)readDAtaFromLocal{NSString * filePath = [[NSBundle mainBundle]pathForResource:@"contacts" ofType:@"plist"];self.dataDic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];NSDictionary * dic = [NSMutableDictionary dictionaryWithDictionary:self.dataDic];//拷貝出來一份給不可變字典 對(duì)不可變字典遍歷NSDictionary * dict = [NSDictionary dictionaryWithDictionary:dic];//獲取拍好序的 keyNSArray * sorted = [[dict allKeys]sortedArrayUsingSelector:@selector(compare:)];self.sortedKeys = [NSMutableArray arrayWithArray:sorted];//不能一邊遍歷結(jié)合一邊操作//外層字典遍歷 // for (NSString * key in dict) { 得到的是無序的for (NSString * key in _sortedKeys) {NSMutableArray * contactArr = [NSMutableArray array];NSArray * group = [dict objectForKey:key];//內(nèi)層遍歷獲取每一個(gè)分組 將 dic 的信息封裝到 Contacts 對(duì)象里面for (NSDictionary * dic in group) {Contacts *per = [[Contacts alloc]initWithDic:dic];//將聯(lián)系人村放到數(shù)組中 [contactArr addObject: per];}//重新把原來的大字典賦拍好序的信息 [self.dataDic setValue:contactArr forKey:key];} } #pragma mark0 --------必須實(shí)現(xiàn)的兩個(gè)方法 //設(shè)置分區(qū)的行數(shù) -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // return [self.dataDic[_sortedKeys[section]] count];//不同的section就是不同的key,代表了不同的組, section的值即為key排好序后的 數(shù)組的下標(biāo)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];//獲取對(duì)應(yīng)的分組NSArray * group = [self.dataDic objectForKey:key];//獲取聯(lián)系人對(duì)象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];//點(diǎn)擊呼叫return cell; } //打電話就條狀下一頁(yè)(這里的功能還沒有實(shí)現(xiàn)?????????????) -(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{} //設(shè)置分區(qū)數(shù) -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return _sortedKeys.count; } //設(shè)置分區(qū)標(biāo)題 -(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 ------------添加系統(tǒng)自帶的編輯按鈕 -(void)coustomNavBar{//添加系統(tǒng)自帶的編輯按鈕 (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 不可編輯//設(shè)置 tableview 的編輯狀態(tài) 目的就是讓 tableView 處于編輯狀態(tài)[(UITableView *)self.view setEditing:editing animated:YES]; } #pragma mark3 -----------提交編輯狀態(tài) 對(duì)數(shù)據(jù)以及界面進(jìn)行處理 (真正的數(shù)據(jù)是放在集合或或數(shù)組,者字典里) //設(shè)置哪些行可以被編輯 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{if (indexPath.row == 1) {//某行能否被修改return YES;}return YES; }#pragma mark4 ------------設(shè)置 tableView 的編輯樣式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{if (indexPath.section == 0) {//設(shè)置第一分組可以添加 一些數(shù)據(jù)return UITableViewCellEditingStyleInsert;//插入樣式 }return UITableViewCellEditingStyleDelete;//刪除樣式 } - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0){return @"點(diǎn)我刪除"; }//提交編輯狀態(tài) 提交編輯狀態(tài)的時(shí)刻 被觸發(fā) -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//獲取到編輯的這一行所在的位置 (key)NSString * key = [self.sortedKeys objectAtIndex:indexPath.section];//獲取到聯(lián)系人的數(shù)組NSMutableArray * group = [_dataDic objectForKey:key];//獲取到對(duì)應(yīng)的聯(lián)系人對(duì)象Contacts * contact = [group objectAtIndex:indexPath.row]; // Contacts * contact = group[indexPath.row];//也可這樣寫//判斷編輯狀態(tài)if (editingStyle == UITableViewCellEditingStyleDelete) { // if (indexPath.row == 2) {//0 // [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; // }//刪除操作//需要判斷是否需要?jiǎng)h除對(duì)應(yīng)的分區(qū),當(dāng)數(shù)組的聯(lián)系人只有一個(gè)的時(shí)候,這時(shí)候需要把該聯(lián)系人對(duì)應(yīng)的分區(qū)也要被刪除if (group.count == 1) {//刪除整個(gè)分區(qū)//1.數(shù)據(jù)源刪除 刪除對(duì)應(yīng)分組的信息[_dataDic removeObjectForKey:key];//刪除對(duì)應(yīng)的分組的數(shù)據(jù)[_sortedKeys removeObject:key];//刪除對(duì)應(yīng)的 key//2.界面上刪除 修改界面 刪除所在分區(qū)的界面 [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationBottom];}else{//刪除該行//1.數(shù)據(jù)源[group removeObject:contact];//刪除聯(lián)系人//2.界面 #warning mark @[indexPath] 什么意思?[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}}//添加編輯狀態(tài)else{//添加操作//1.數(shù)據(jù)源操作NSDictionary * dic = @{@"name":@"白白",@"gender":@"",@"phoneNum":@"12345625602",@"photo":@"uuuuuu"};//將字典封裝成聯(lián)系人對(duì)象Contacts * newPer = [[Contacts alloc]initWithDic:dic];//將聯(lián)系人添加到對(duì)應(yīng)的分區(qū)的聯(lián)系人數(shù)組里[group insertObject:newPer atIndex:indexPath.row];//添加一個(gè)聯(lián)系人//2.界面操作[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];//界面上添加一個(gè)聯(lián)系人 [newPer release];} }// PM #pragma mark5 ------------設(shè)置 tableView 的cell 的移動(dòng) //移動(dòng)(設(shè)置某些行的 cell 可以移動(dòng))(先打一個(gè) BOOl 尋找方法) -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{return YES; } //提交移動(dòng)的操作 (先打 void 再尋找方法) -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{//該方法 sourceIndexPath 是原來的區(qū)域 destinationIndexPath 是移動(dòng)后的區(qū)域//做移動(dòng)操作的時(shí)候,界面上已經(jīng)發(fā)生了改變.所以我們只需要處理數(shù)據(jù)上的//獲取分組的數(shù)組//獲取外層大字典的 keyNSString * key = [self.sortedKeys objectAtIndex:sourceIndexPath.section];NSMutableArray * group = [_dataDic objectForKey:key];//這里寫 retain 的原因就是讓其引用計(jì)數(shù)器加1,保持所有權(quán)Contacts * per = [[group objectAtIndex:sourceIndexPath.row]retain];//讓引用計(jì)數(shù)加1,保證對(duì)象的存在//從數(shù)組中把對(duì)應(yīng)的 per 對(duì)象從數(shù)組中刪除 [group removeObjectAtIndex:sourceIndexPath.row];//然后再把對(duì)應(yīng)的對(duì)象移動(dòng)到目的位置 [group insertObject:per atIndex:destinationIndexPath.row];[per release]; } //限定 cell 的移動(dòng)界限 ----禁止跨區(qū)移動(dòng) -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{//tableView 當(dāng)前操作的 tableview//sourceIndexPath 移動(dòng)之前的下標(biāo)索引值 就是移動(dòng)之前的 cell 的位置//proposedDestinationIndexPath 移動(dòng)之后所得到的目的位置//如果移動(dòng)之前 和 移動(dòng)之后的所在分區(qū)是同一個(gè)分區(qū),則支持移動(dòng)if ( sourceIndexPath.section == proposedDestinationIndexPath.section) {return proposedDestinationIndexPath;//移動(dòng)的位置}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 的區(qū)別1.前者的根視圖是 tableView 后者是 UIView2.如果用 UIView 的話 需要再設(shè)置 dataSource ,前者不用再設(shè)置 dataSource 和 delegate ,同時(shí)也不用再服從協(xié)議,因?yàn)樽陨硪呀?jīng)服從了, 而后者我們需要指定他的 dataSource 和 delegate3.前者不用重寫 setEdting:Animation : 方法控制 tabelview,后者需要指定4.前者已經(jīng)自動(dòng)的幫我們生成了對(duì)應(yīng)的 dataSource 的最基本最常用的協(xié)議的方法(需要使用,就注開就可以了),后者需要自己去手動(dòng)添加相應(yīng)的協(xié)議方法*/ - (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調(diào)用系統(tǒng)的相冊(cè),查詢相冊(cè)UIDatePickerUIPickerView繪圖 DrawRectUITextViewUIToolBar*/- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {//設(shè)置重用標(biāo)志符static NSString * identifier = @"cell";//根據(jù)重用的標(biāo)志符在 重用列表中取 cellUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//如果沒有獲取重用的 cell ,則新建if (!cell) {cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];}// Configure the cell...cell.textLabel.text = @"時(shí)間廣場(chǎng)";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;//當(dāng)繪制底層的邊界的時(shí)候,本控件也和邊界一起繪制 [_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詳細(xì)信息"; } -(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{//繪制圖片//創(chuàng)建一個(gè) bitmap 的上下文,并指定為當(dāng)前使用的 context UIGraphicsBeginImageContext(size);//根據(jù)外界傳入的大小繪制改變大小后的圖片[self drawInRect:CGRectMake(0, 0, size.width, size.height)];//從當(dāng)前的 context 獲取改變大小后的圖片UIImage * scaleImage = UIGraphicsGetImageFromCurrentImageContext();//使我們當(dāng)前的 context 從棧頂出棧 UIGraphicsEndImageContext();//返回改變大小后的圖片return scaleImage; } @end View Code?UIImage+Scale.m

?

//為UItableView 的 cell 里添加一組相同規(guī)格的圖片的時(shí)候用到

#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{//繪制圖片//創(chuàng)建一個(gè) bitmap 的上下文,并指定為當(dāng)前使用的 context UIGraphicsBeginImageContext(size);//根據(jù)外界傳入的大小繪制改變大小后的圖片[self drawInRect:CGRectMake(0, 0, size.width, size.height)];//從當(dāng)前的 context 獲取改變大小后的圖片UIImage * scaleImage = UIGraphicsGetImageFromCurrentImageContext();//使我們當(dāng)前的 context 從棧頂出棧 UIGraphicsEndImageContext();//返回改變大小后的圖片return scaleImage; } @end View Code?UIImage+Scale.m

?

轉(zhuǎn)載于:https://www.cnblogs.com/benpaobadaniu/p/4799896.html

總結(jié)

以上是生活随笔為你收集整理的UI:UITableView 编辑、cell重用机制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。