ListTableViewController.m
//
// ListTableViewController.m
// Test
//
//
#import "ListTableViewController.h"
#import "EditViewController.h"
#import "Contact.h"
#import "EditViewController.h"
#import "AddViewController.h"
@interface ListTableViewController ()<EditViewControllerDelegate,AddViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tbView;
@property (nonatomic,strong) NSMutableArray *contacts;
@end
@implementation ListTableViewController
//初始化Contact对象数组
-(NSMutableArray *)contacts{
if (!_contacts) {
_contacts = [NSMutableArray array];
[_contacts addObject:[[Contact alloc] initWithName:@"AAA" AndTel:@"13656569656"]];
[_contacts addObject:[[Contact alloc] initWithName:@"BBB" AndTel:@"13656569600"]];
[_contacts addObject:[[Contact alloc] initWithName:@"CCC" AndTel:@"13656569611"]];
[_contacts addObject:[[Contact alloc] initWithName:@"DDD" AndTel:@"13656569622"]];
}
return _contacts;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
//navigationController 预跳转处理
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
id destVc =segue.destinationViewController;
if([destVc isKindOfClass:[EditViewController class]])
{
//修改界面
EditViewController *editVc =destVc;
NSInteger index = self.tableView.indexPathForSelectedRow.row;
editVc.contact=self.contacts[index];
editVc.delegate=self;
}else if([destVc isKindOfClass:[AddViewController class]]){
//添加界面
AddViewController *addVc = destVc;
addVc.delegate=self;
}
}
//关闭添加页面,并且显示数据到列表页
-(void)addViewController:(AddViewController *)addVc didFinishSaveContact:(Contact *)contact{
//将添加界面返回的 对象添加到数组中
[self.contacts addObject:contact];
//显示到列表
NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.contacts.count-1 inSection:0];
[self.tbView insertRowsAtIndexPaths:@[lastIndex] withRowAnimation:UITableViewRowAnimationFade];
//关闭添加界面
[self.navigationController popToRootViewControllerAnimated:YES];
}
//关闭修改界面,刷新列表页
-(void)editViewController:(EditViewController *)editVc didFinishSaveContact:(Contact *)contact{
//关闭修改界面
[[self navigationController ]popViewControllerAnimated:YES];
//更新列表页对应行数据
NSInteger rowIndex = [self.contacts indexOfObject:contact];
NSIndexPath *refreshIndex = [NSIndexPath indexPathForRow:rowIndex inSection:0];
[self.tbView reloadRowsAtIndexPaths:@[refreshIndex] withRowAnimation:UITableViewRowAnimationFade];
}
#pragma mark UITableView 操作
//设置表格有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.contacts.count;
}
//填充单元格数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identity =@"cell";
//使用UITableViewCell控件方式显示数据
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity forIndexPath:indexPath];
Contact *contact =_contacts[indexPath.row];
cell.textLabel.text=contact.name;
cell.detailTextLabel.text=contact.tel;
//显示勾选图标
//cell.accessoryType=UITableViewCellAccessoryCheckmark;
//使用xib自定义row显示
/*
ContactCell *cell=(ContactCell *)[tableView dequeueReusableCellWithIdentifier:identity];
if(cell==nil)
{
//从xib视图加载单元格
NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"ContactCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.labname.text=contact.name;
cell.labtel.text=contact.tel;
*/
return cell;
}
//获取选中的单元格
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
//得到选中的单元格
//ContactCell *cell =(SelfRowCell *)[tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType==UITableViewCellAccessoryNone)
{
//获取单元格文本
//NSString *str=cell.textLabel.text;
//设置勾选
cell.accessoryType=UITableViewCellAccessoryCheckmark;
//显示选中行的textlable的text
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:cell.textLabel.text delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
}
}
//删除行
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[_contacts removeObjectAtIndex:indexPath.row];
[_contacts removeObjectAtIndex:indexPath.row];
[self.tbView reloadData];
}
//改变删除按钮的文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
@end
EditViewController.h
#import <UIKit/UIKit.h>
@class Contact,EditViewController;
@protocol EditViewControllerDelegate <NSObject>
-(void)editViewController:(EditViewController *)editVc didFinishSaveContact:(Contact *)contact;
@end
@interface EditViewController : UIViewController
@property (nonatomic,strong)Contact *contact;
@property(weak,nonatomic)id<EditViewControllerDelegate>delegate;
@end
EditViewController.m
#import "EditViewController.h"
#import "Contact.h"
@interface EditViewController ()
@property (weak,nonatomic)IBOutlet UITextField *name;
@property (weak,nonatomic)IBOutlet UITextField *tel;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *editBar;
@property(weak,nonatomic)IBOutlet UIButton *btnSave;
-(IBAction)btnSaveClick:(UIButton *)sender;
- (IBAction)editClick:(UIBarButtonItem *)sender;
@end
@implementation EditViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.name.text = self.contact.name;
self.tel.text = self.contact.tel;
self.btnSave.hidden=YES;
self.name.enabled=NO;
self.tel.enabled=NO;
}
-(void)btnSaveClick:(UIButton *)sender{
if(self.name.text.length>0 && self.tel.text.length>0){
if ([self.delegate respondsToSelector:@selector(editViewController:didFinishSaveContact:)]) {
self.contact.name = self.name.text;
self.contact.tel = self.tel.text;
[self.delegate editViewController:self didFinishSaveContact:self.contact];
}
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"name or tel can't null " delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alert show];
}
}
- (IBAction)editClick:(UIBarButtonItem *)sender {
self.name.enabled=!self.name.enabled;
self.tel.enabled=!self.tel.enabled;
self.btnSave.hidden = !self.btnSave.hidden;
if (self.name.enabled==YES) {
sender.title=@"cancel";
}else
{
sender.title=@"edit";
}
}
@end
AddViewController.h
#import <UIKit/UIKit.h>
@class AddViewController,Contact;
@protocol AddViewControllerDelegate <NSObject>
-(void)addViewController:(AddViewController *)addVc didFinishSaveContact:(Contact *)contact;
@end
@interface AddViewController : UIViewController
@property(weak,nonatomic)id<AddViewControllerDelegate>delegate;
@end
AddViewController.m
#import "AddViewController.h"
#import "Contact.h"
@interface AddViewController ()
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *tel;
- (IBAction)btnSaveClick:(UIButton *)sender;
@end
@implementation AddViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)btnSaveClick:(UIButton *)sender {
if(self.name.text.length>0 && self.tel.text.length>0){
Contact *contact = [[Contact alloc] initWithName:self.name.text AndTel:self.tel.text];
[self.delegate addViewController:self didFinishSaveContact:contact];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"name or tel can't null " delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alert show];
}
}
@end
Contact.h
#import <Foundation/Foundation.h>
@interface Contact : NSObject
-(instancetype)initWithName:(NSString *)name AndTel:(NSString *)tel;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *tel;
@end
Contact.m
#import "Contact.h"
@implementation Contact
-(instancetype)initWithName:(NSString *)name AndTel:(NSString *)tel
{
Contact *contact = [[Contact alloc] init];
contact.name=name;
contact.tel=tel;
return contact;
}
@end