UI第十二講 通信錄實戰

//如下爲通信錄完整代碼,另附有構建框架,綜合性較強,望多加練習!

ListViewController.m文件內容以下:

#import "LIstViewController.h"
#import "DetailViewController.h"
#import "ListTableViewCell.h"

@interface LIstViewController ()<UITableViewDataSource,UITableViewDelegate,DetailVCDelegate> @property(nonatomic,strong)UITableView *tableView; @property(nonatomic,strong)NSMutableArray *dataArray; @end

@implementation LIstViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.
 [self loadTableView]; //數組只有初始化後才能使用**************
    self.dataArray = [NSMutableArray array]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(rightBarButtonClick:)]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftBarButtonClick:)]; } //按鈕點擊事件以及按鈕添加刪除功能的實現--------------------------------------------------
-(void)rightBarButtonClick:(UIBarButtonItem *)bar { [self.tableView setEditing:!self.tableView.isEditing animated:YES]; } -(void)leftBarButtonClick:(UIBarButtonItem *)bar { DetailViewController *detailVC = [[DetailViewController alloc]init]; //數據傳入第一個界面的代理
    detailVC.delegate = self; [self.navigationController pushViewController:detailVC animated:YES]; } //代理方法的實現
-(void)passValueDelegate:(AddressBookModel *)passModel { //代理方法傳過來的值添加到數組中
 [self.dataArray addObject:passModel]; //刷新tableView
 [self.tableView reloadData]; } // 設置哪一個cell能夠編輯
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return self; } //真正的執行編輯的事情
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //在編輯時,以必定要先處理好數據源,而後再處理view
 [self.dataArray removeObjectAtIndex:indexPath.row]; //tableView 刪除一個cell的方法,第一個參數表明 刪除哪一個分區的cell 第二個參數表明 刪除時的動畫
 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } //點擊cell,返回第二個界面進行信息的修改
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *detailVC = [[DetailViewController alloc] init]; //屬性賦值第二步 把dataArray賦值給屬性傳值的對象
    detailVC.model = self.dataArray[indexPath.row]; //修改值的代理
    detailVC.delegate = self; //傳遞indexPath.row的做用是再傳回來,做爲代理方法中的參數使用
    detailVC.index = indexPath.row; [self.navigationController pushViewController:detailVC animated:YES]; } //修改值的代理方法
-(void)changeValueToListVC:(AddressBookModel *)passModel index:(NSInteger)index { //替換數組 index位置的元素 爲新的model
 [self.dataArray replaceObjectAtIndex:index withObject:passModel]; // 刷新tableView
 [self.tableView reloadData]; } //tableView的基本設置------------------------------------
-(void)loadTableView { self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } -(ListTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *str = @"CELL"; ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if (cell == nil) { cell = [[ListTableViewCell alloc ]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]; } //這裏用model接收數據******************
    cell.model = self.dataArray[indexPath.row]; return cell; } //cell的總高以及自適應高度----------------------------------- //自適應高度 獲取文本方法 此方法須要從新調用 *******************************
-(CGFloat)stringHeightWithString:(NSString *)str fontSize:(CGFloat)fontSize contentSize:(CGSize)size { //第一個參數,表明最大的範圍 //第二個參數 表明是否考慮字體和字號 //第三個參數 表明是使用什麼字體和字號 //第四個參數 用不到 基本上是 nil
    CGRect  stringRect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil]; NSLog(@"cgrect ==== %f", stringRect.size.height); return stringRect.size.height; } //cell總高計算
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { AddressBookModel *model = self.dataArray[indexPath.row]; return 150 + [self stringHeightWithString:model.content fontSize:18 contentSize:CGSizeMake(self.view.frame.size.width - 20, 10000)]; }
 

 

 ListTableViewCell.m文件數組

 

 
 
#import "ListTableViewCell.h"

@interface ListTableViewCell () @property(nonatomic,strong)UILabel *nameLable; @property(nonatomic,strong)UILabel *ageLable; @property(nonatomic,strong)UILabel *sexLable; @property(nonatomic,strong)UILabel *phoneLable; @property(nonatomic,strong)UILabel *contentLable; @property(nonatomic,strong)UIImageView *heardImageView; @end



@implementation ListTableViewCell -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:(NSString *)reuseIdentifier]; if (self) { self.nameLable = [[UILabel alloc] init]; [self.contentView addSubview:self.nameLable]; self.ageLable = [[UILabel alloc] init]; [self.contentView addSubview:self.ageLable]; self.sexLable = [[UILabel alloc] init]; [self.contentView addSubview:self.sexLable]; self.phoneLable = [[UILabel alloc] init]; [self.contentView addSubview:self.phoneLable]; self.contentLable= [[UILabel alloc] init]; [self.contentView addSubview:self.contentLable]; self.heardImageView = [[UIImageView alloc] init]; [self.contentView addSubview:self.heardImageView]; } return self; } //在這裏面寫全部的控件佈局
-(void)layoutSubviews { [super layoutSubviews]; self.heardImageView.frame = CGRectMake(10, 10, 100, 100); self.nameLable.frame = CGRectMake(120, 10, self.frame.size.width - 130, 20); self.ageLable.frame = CGRectMake(120, 40, self.frame.size.width - 130, 20); self.sexLable.frame = CGRectMake(120, 70, self.frame.size.width - 130, 20); self.phoneLable.frame = CGRectMake(120, 90, self.frame.size.width - 130, 20); self.contentLable.frame = CGRectMake(10, 120, self.frame.size.width - 20, 20); self.contentLable.numberOfLines = 0; [self.contentLable sizeToFit]; } //重寫model的setter方法 將model接收到的值,賦給自己的lable*********************
-(void)setModel:(AddressBookModel *)model { self.nameLable.text = model.name; self.ageLable.text = model.age; self.sexLable.text = model.sex; self.phoneLable.text = model.phone; self.contentLable.text = model.content; // 把NSData轉換成image類型 ****
    self.heardImageView.image = [UIImage imageWithData:model.image]; } @end
 

 

 DetailViewController.h文件框架

 

 
 
#import <UIKit/UIKit.h>
#import "AddressBookModel.h"

@protocol DetailVCDelegate <NSObject>

//傳值的代理方法
-(void)passValueDelegate:(AddressBookModel *)passModel; //修改的代理方法
-(void)changeValueToListVC:(AddressBookModel *)passModel index:(NSInteger)index; @end



@interface DetailViewController : UIViewController //代理傳值的屬性1
@property(nonatomic,assign)id<DetailVCDelegate>delegate; //屬性傳值
@property(nonatomic,strong)AddressBookModel *model; //代理傳值的屬性2
@property(nonatomic,assign)NSInteger index; @end
 

 

DetailViewController.m文件ide

 

 
 
#import "DetailViewController.h"
#import "LTView.h"

@interface DetailViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate> @property(nonatomic,strong)LTView *nameView; @property(nonatomic,strong)LTView *sexView; @property(nonatomic,strong)LTView *ageView; @property(nonatomic,strong)LTView *phoneView; //文本視圖
@property(nonatomic,strong)UITextView *contentView; @property(nonatomic,strong)UIButton *imageButton; //設置照片屬性
@property(nonatomic,strong)NSData *imageDate; @end

@implementation DetailViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.
 [self loadLTView]; self.navigationController.navigationBar.translucent = NO; } //注意起名字時不能和系統名衝突 //初始化view
-(void)loadLTView { //照片按鈕的設置
    self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.imageButton setTitle:@"選取照片" forState:UIControlStateNormal]; self.imageButton.backgroundColor = [UIColor grayColor]; self.imageButton.frame = CGRectMake(self.view.frame.size.width/2 - 50 , 10, 100, 100); [self.imageButton addTarget:self action:@selector(imagePickerButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.imageButton]; // 把前一頁面傳入的NSData類型的數據轉換成image ***
 [self.imageButton setImage:[UIImage imageWithData:self.model.image] forState:UIControlStateNormal]; 
    //這裏要保證,在修改時把圖片從第一個頁面保存到第二個頁面,同時可以保存圖片*********
    self.imageDate = self.model.image; self.nameView
= [[LTView alloc] initWithFrame:CGRectMake(10, 110, self.view.frame.size.width - 20, 30)]; self.nameView.nameLable.text = @"姓名"; //屬性傳值中的賦值 self.nameView.messageTextField.text = self.model.name; self.nameView.messageTextField.placeholder = @"請輸入姓名"; [self.view addSubview:self.nameView]; self.ageView = [[LTView alloc] initWithFrame:CGRectMake(10, 150, self.view.frame.size.width - 20, 30)]; self.ageView.nameLable.text = @"年齡"; self.ageView.messageTextField.text = self.model.age; self.ageView.messageTextField.placeholder = @"請輸入年齡"; [self.view addSubview:self.ageView]; self.sexView = [[LTView alloc] initWithFrame:CGRectMake(10, 190, self.view.frame.size.width - 20, 30)]; self.sexView.nameLable.text = @"性別"; //屬性傳值中的賦值 self.sexView.messageTextField.text = self.model.sex ; self.sexView.messageTextField.placeholder = @"請輸入性別"; [self.view addSubview:self.sexView]; self.phoneView = [[LTView alloc] initWithFrame:CGRectMake(10, 230, self.view.frame.size.width - 20, 30)]; self.phoneView.nameLable.text = @"手機"; //屬性傳值中的賦值 self.phoneView.messageTextField.text = self.model.phone; self.phoneView.messageTextField.placeholder = @"請輸入手機號"; [self.view addSubview:self.phoneView]; self.contentView = [[UITextView alloc] initWithFrame:CGRectMake(10, 270, self.view.frame.size.width -20, 150)]; //屬性傳值中的賦值 self.contentView.text = self.model.content; self.contentView.backgroundColor = [UIColor orangeColor]; [self.view addSubview:self.contentView]; UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom]; saveButton.frame = CGRectMake(10, 430, self.view.frame.size.width - 20, 30); [saveButton setTitle:@"保存" forState:UIControlStateNormal]; saveButton.backgroundColor = [UIColor greenColor]; //設置按鈕圓角 saveButton.layer.borderWidth = 3.0f; //按鈕點擊事件 [saveButton addTarget:self action:@selector(saveButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:saveButton]; } //按鈕點擊事件 -(void)saveButtonClicked:(UIButton *)sender { //獲取到全部可輸入控件上的輸入的數據 NSString *nameStr = self.nameView.messageTextField.text; NSString *ageStr = self.ageView.messageTextField.text; NSString *sexStr = self.sexView.messageTextField.text; NSString *phoneStr = self.phoneView.messageTextField.text; NSString *contentStr = self.contentView.text; //初始化一個model 傳輸照片時,要注意這裏的格式*** AddressBookModel *model = [[AddressBookModel alloc] initWithName:nameStr age:ageStr sex:sexStr pnone:phoneStr content:contentStr image:self.imageDate]; // 判斷代理人是否存在,代理人是否註冊了passValueDelegate 和 if (self.delegate && [self.delegate respondsToSelector:@selector(passValueDelegate:)] && [self.delegate respondsToSelector:@selector(changeValueToListVC:index:)]) { // 若是self.model是否存在,若是存在 則斷定爲是修改聯繫人,因此 觸發changeValueToListVC:index:方法 if (self.model != nil) { // 填入的兩個參數,第一個是model 注意 這個model是本身建立的 不是屬性的model 第二個參數 index。告訴上一個頁面須要替換數組中哪一個位置的元素 [self.delegate changeValueToListVC:model index:self.index]; } else{ // 直接把本身建立的model 返回去 [self.delegate passValueDelegate:model]; } } [self.navigationController popToRootViewControllerAnimated:YES]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } //照片的設置------------------------------------------------------------ //圖片按鈕的點擊事件 -(void)imagePickerButtonClicked:(UIButton *)sender { UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"選取照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相機",@"相冊", nil]; [action showFromRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) inView:self.view animated:YES]; } -(void)setImagePickerController:(UIImagePickerControllerSourceType)source { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = source; imagePicker.delegate = self; [imagePicker setEditing:YES]; imagePicker.allowsEditing = YES; //模態推出視圖 [self presentViewController:imagePicker animated:YES completion:nil]; } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: [self setImagePickerController:UIImagePickerControllerSourceTypeCamera]; break; case 1: [self setImagePickerController:UIImagePickerControllerSourceTypePhotoLibrary]; break; default: break; } } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //若是圖片編輯以後 須要使用 UIImagePickerControllerEditedImage //若是圖片沒有編輯 只需獲取 UIImagePickerControllerOriginalImage // UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; //UIImagePNGRepresentation 把圖片轉化成PNG格式 不壓縮比較大 // UIImageJPEGRepresentation 把圖片轉化成JPEG的格式,第二個參數填的是一個壓縮的係數,通常是 0.5 // NSDate *data = UIImagePNGRepresentation(<#UIImage *image#>); //照片傳遞到上一界面時,這裏要進行數據的轉化*** self.imageDate = UIImageJPEGRepresentation(image, 0.5); [self.imageButton setImage:image forState:UIControlStateNormal]; [picker dismissViewControllerAnimated:YES completion:nil]; }
 
 

 

 

 

LTView.h文件佈局

#import <UIKit/UIKit.h>

@interface LTView : UIView @property(nonatomic,strong)UILabel *nameLable; @property(nonatomic,strong)UITextField *messageTextField; @end

 

 

 LTView.m文件字體

 

 
 
#import "LTView.h"

@implementation LTView -(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.nameLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)]; //設置字體的大小
        self.nameLable.font = [UIFont systemFontOfSize:15]; [self addSubview:self.nameLable]; //字體居中
        self.nameLable.textAlignment = NSTextAlignmentCenter; //設置邊框寬度
        self.nameLable.layer.borderWidth = 0.5; self.messageTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 0, self.frame.size.width - 50, 30)]; //設置邊框寬度
        self.messageTextField.layer.borderWidth = 0.5; [self addSubview:self.messageTextField]; } return self; }
 

 

AddressBookModel.h文件動畫

 

 
 
#import <Foundation/Foundation.h>

@interface AddressBookModel : NSObject @property(nonatomic,copy)NSString *name; @property(nonatomic,copy)NSString *age; @property(nonatomic,copy)NSString *sex; @property(nonatomic,copy)NSString *phone; @property(nonatomic,copy)NSString *content; @property(nonatomic,copy)NSData *image; -(id)initWithName:(NSString *)nameStr age:(NSString *)age sex:(NSString *)sex pnone:(NSString *)phone content:(NSString *)content image:(NSData *)image; @end
 

 

AddressBookModel.h文件atom

 

 
 
#import "AddressBookModel.h"

@implementation AddressBookModel -(id)initWithName:(NSString *)nameStr age:(NSString *)age sex:(NSString *)sex pnone:(NSString *)phone content:(NSString *)content image:(NSData *)image { self = [super init]; if (self) { _name = nameStr; _age = age; _sex = sex; _phone = phone; _content = content; _image = image; } return self; } @end
 

 

 

如下爲總體框架的搭建:spa

 

 

 

效果圖:(有點簡陋,但基本功能都已實現,添加,刪除,修改,添加圖片等)代理

 

 

 

 

 

通信錄總體思路框架結構圖:code

 

相關文章
相關標籤/搜索