UiTableView實現手機通訊錄聯絡人頁面,並能實現頁面跳轉和修改
阿新 • • 發佈:2021-01-21
主頁面:
// // ViewController.m // myUItest // // Created by bytedance on 2021/1/19. // #import "ViewController.h" #import "Contact.h" #import "KCContactGroup.h" #import "detailViewController.h" #import <Masonry/Masonry.h> #import "secondViewController.h" #import <Foundation/Foundation.h> #import <objc/runtime.h> #import "MyTableViewCell.h" @interface ViewController ()<UITableViewDataSource, UITableViewDelegate> @property UITableView *tableView; @property NSMutableArray *contacts; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self initData]; self.tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; self.tableView.dataSource=self; self.tableView.delegate = self; [self.view addSubview:self.tableView]; } -(void)initData { self.contacts=[[NSMutableArray alloc]init]; KCContact *contact1=[KCContact initWithFirstName:@"Cui" andLastName:@"Kenshin" andPhoneNumber:@"18500131234"]; KCContact *contact2=[KCContact initWithFirstName:@"Cui" andLastName:@"Tom" andPhoneNumber:@"18500131237"]; KCContactGroup *group1=[KCContactGroup initWithName:@"C" andDetail:@"With names beginning with C" andContacts:[NSMutableArray arrayWithObjects:contact1,contact2, nil]]; [self.contacts addObject:group1]; KCContact *contact3=[KCContact initWithFirstName:@"Lee" andLastName:@"Terry" andPhoneNumber:@"18500131238"]; KCContact *contact4=[KCContact initWithFirstName:@"Lee" andLastName:@"Jack" andPhoneNumber:@"18500131239"]; KCContact *contact5=[KCContact initWithFirstName:@"Lee" andLastName:@"Rose" andPhoneNumber:@"18500131240"]; KCContactGroup *group2=[KCContactGroup initWithName:@"L" andDetail:@"With names beginning with L" andContacts:[NSMutableArray arrayWithObjects:contact3,contact4,contact5, nil]]; [self.contacts addObject:group2]; KCContact *contact6=[KCContact initWithFirstName:@"Sun" andLastName:@"Kaoru" andPhoneNumber:@"18500131235"]; KCContact *contact7=[KCContact initWithFirstName:@"Sun" andLastName:@"Rosa" andPhoneNumber:@"18500131236"]; KCContactGroup *group3=[KCContactGroup initWithName:@"S" andDetail:@"With names beginning with S" andContacts:[NSMutableArray arrayWithObjects:contact6,contact7, nil]]; [self.contacts addObject:group3]; KCContact *contact8=[KCContact initWithFirstName:@"Wang" andLastName:@"Stephone" andPhoneNumber:@"18500131241"]; KCContact *contact9=[KCContact initWithFirstName:@"Wang" andLastName:@"Lucy" andPhoneNumber:@"18500131242"]; KCContact *contact10=[KCContact initWithFirstName:@"Wang" andLastName:@"Lily" andPhoneNumber:@"18500131243"]; KCContact *contact11=[KCContact initWithFirstName:@"Wang" andLastName:@"Emily" andPhoneNumber:@"18500131244"]; KCContact *contact12=[KCContact initWithFirstName:@"Wang" andLastName:@"Andy" andPhoneNumber:@"18500131245"]; KCContactGroup *group4=[KCContactGroup initWithName:@"W" andDetail:@"With names beginning with W" andContacts:[NSMutableArray arrayWithObjects:contact8,contact9,contact10,contact11,contact12, nil]]; [self.contacts addObject:group4]; KCContact *contact13=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joy" andPhoneNumber:@"18500131246"]; KCContact *contact14=[KCContact initWithFirstName:@"Zhang" andLastName:@"Vivan" andPhoneNumber:@"18500131247"]; KCContact *contact15=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joyse" andPhoneNumber:@"18500131248"]; KCContactGroup *group5=[KCContactGroup initWithName:@"Z" andDetail:@"With names beginning with Z" andContacts:[NSMutableArray arrayWithObjects:contact13,contact14,contact15, nil]]; [self.contacts addObject:group5]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { secondViewController *subPage=[[secondViewController alloc]init]; // subPage.selectedIndexPath = ; KCContactGroup *group=self.contacts[indexPath.section]; KCContact *contact=group.contacts[indexPath.row]; subPage.detailFrommain =contact; subPage.callback = ^(NSIndexPath *indexPath, KCContact *newDetails) { [group.contacts replaceObjectAtIndex:indexPath.row withObject:newDetails]; [self.contacts replaceObjectAtIndex:indexPath.section withObject:group]; [self.tableView reloadData]; }; [self.navigationController pushViewController:subPage animated:YES]; } #pragma mark - 資料來源方法 #pragma mark 返回分組數 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ NSLog(@"計算分組數"); return self.contacts.count; } #pragma mark 返回每組行數 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSLog(@"計算每組(組%li)行數",(long)section); KCContactGroup *group1=self.contacts[section]; return group1.contacts.count; } #pragma mark 返回每行的單元格 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //NSIndexPath是一個結構體,記錄了組和行資訊 NSLog(@"生成單元格(組:%li,行%li)",(long)indexPath.section,(long)indexPath.row); KCContactGroup *group=self.contacts[indexPath.section]; KCContact *contact=group.contacts[indexPath.row]; MyTableViewCell *cell=[[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; cell.indexPath = indexPath; cell.textLabel.text=[contact getName]; cell.detailTextLabel.text=contact.phoneNumber; // UIButton *detail = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // [cell addSubview:detail]; // [detail mas_makeConstraints:^(MASConstraintMaker *make) { // make.right.equalTo(cell).offset(-20); // make.top.equalTo(cell).offset(5); // }]; /*[detail mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(cell).with.insets(UIEdgeInsetsMake(10, 10, 10, 10)); }];*/ // [detail setTitle:@"details" forState:UIControlStateNormal]; //detail.backgroundColor=[UIColor blueColor]; // detail.tag=indexPath.section*10+indexPath.row; // [detail addTarget:self action:@selector(jump:) // forControlEvents:UIControlEventTouchUpInside]; return cell; } #pragma mark 返回每組頭標題名稱 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ NSLog(@"生成組(組%li)名稱",(long)section); KCContactGroup *group=self.contacts[section]; return group.name; } #pragma mark 返回每組尾部說明 -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ NSLog(@"生成尾部(組%li)詳情",(long)section); KCContactGroup *group=_contacts[section]; return group.detail; } #pragma mark 返回每組標題索引 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ NSLog(@"生成組索引"); NSMutableArray *indexs=[[NSMutableArray alloc]init]; for(KCContactGroup *group in _contacts){ [indexs addObject:group.name]; } return indexs; } -(IBAction)jump:(MyTableViewCell *)cell { NSLog(@"被按下了"); /*[self presentViewController:[[detailViewController alloc]init] animated:true completion:^(void) { }];*/ NSLog(@"搞定了"); } @end
建立的聯絡人類和實現
// // Contact.h // myUItest // // Created by bytedance on 2021/1/19. // #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface KCContact : NSObject #pragma mark 姓 @property (nonatomic,copy) NSString *firstName; #pragma mark 名 @property (nonatomic,copy) NSString *lastName; #pragma mark 手機號碼 @property (nonatomic,copy) NSString *phoneNumber; #pragma mark 帶引數的建構函式 -(KCContact *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber; #pragma mark 取得姓名 -(NSString *)getName; #pragma mark 帶引數的靜態物件初始化方法 +(KCContact *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber; @end NS_ASSUME_NONNULL_END
// // Contact.m // myUItest // // Created by bytedance on 2021/1/19. // #import "Contact.h" @implementation KCContact -(KCContact *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber{ if(self=[super init]){ self.firstName=firstName; self.lastName=lastName; self.phoneNumber=phoneNumber; } return self; } -(NSString *)getName{ return [NSString stringWithFormat:@"%@ %@",_lastName,_firstName]; } +(KCContact *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber{ KCContact *contact1=[[KCContact alloc]initWithFirstName:firstName andLastName:lastName andPhoneNumber:phoneNumber]; return contact1; } @end
分組聯絡人定義和實現
//
// KCContactGroup.h
// myUItest
//
// Created by bytedance on 2021/1/19.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface KCContactGroup : NSObject
#pragma mark 組名
@property (nonatomic,copy) NSString *name;
#pragma mark 分組描述
@property (nonatomic,copy) NSString *detail;
#pragma mark 聯絡人
@property (nonatomic,strong) NSMutableArray *contacts;
#pragma mark 帶引數個建構函式
-(KCContactGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts;
#pragma mark 靜態初始化方法
+(KCContactGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts;
@end
NS_ASSUME_NONNULL_END
//
// KCContactGroup.m
// myUItest
//
// Created by bytedance on 2021/1/19.
//
#import "KCContactGroup.h"
@implementation KCContactGroup
-(KCContactGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts{
if (self=[super init]) {
self.name=name;
self.detail=detail;
self.contacts=contacts;
}
return self;
}
+(KCContactGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts{
KCContactGroup *group1=[[KCContactGroup alloc]initWithName:name andDetail:detail andContacts:contacts];
return group1;
}
@end
自定義UiTableviewCell
//
// MyTableViewCell.h
// test
//
// Created by bytedance on 2021/1/21.
//
#import <UIKit/UIKit.h>
typedef void(^CellBlock)(NSIndexPath * _Nullable indexPath);
NS_ASSUME_NONNULL_BEGIN
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, copy) CellBlock cellBlock;
@end
NS_ASSUME_NONNULL_END
//
// MyTableViewCell.m
// test
//
// Created by bytedance on 2021/1/21.
//
#import "MyTableViewCell.h"
#import <Masonry/Masonry.h>
@implementation MyTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIButton *detail = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[detail addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[detail setTitle:@"details" forState:UIControlStateNormal];
[self.contentView addSubview:detail];
[detail mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.contentView).offset(-20);
make.top.equalTo(self.contentView).offset(5);
}];
}
return self;
}
- (void)buttonAction:(id)sender {
if (self.cellBlock) {
self.cellBlock(self.indexPath);
}
}
@end
頁面二的定義和實現
//
// secondViewController.h
// test
//
// Created by bytedance on 2021/1/20.
//
#import <UIKit/UIKit.h>
#import "Contact.h"
typedef void(^Callback)(NSIndexPath * _Nullable indexPath, KCContact * _Nonnull newDetails);
NS_ASSUME_NONNULL_BEGIN
@interface secondViewController : UIViewController
@property(strong,nonatomic) KCContact * detailFrommain;
@property(strong,nonatomic) KCContact * detailBackmain;
@property(strong,nonatomic) UITextField *firstNameView;
@property(strong,nonatomic) UITextField *lastNameView;
@property(strong,nonatomic) UITextField *phoneView;
@property(nonatomic, copy) Callback callback;
@property(nonatomic, strong) NSIndexPath *selectedIndexPath;
@end
NS_ASSUME_NONNULL_END
//
// secondViewController.m
// test
//
// Created by bytedance on 2021/1/20.
//
#import "secondViewController.h"
#import <Masonry/Masonry.h>
#import "Contact.h"
@interface secondViewController ()
@end
@implementation secondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor whiteColor];
CGFloat baseoffset=80;
CGFloat margin=10;
self.firstNameView=[[UITextField alloc]init];
self.firstNameView.borderStyle=UITextBorderStyleRoundedRect;
self.firstNameView.text=self.detailFrommain.firstName;
[self.view addSubview:self.firstNameView];
[self.firstNameView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.top.equalTo(self.view).offset(baseoffset);
make.width.equalTo(self.view).multipliedBy(0.55);
make.height.equalTo(self.view).multipliedBy(0.1);
}];
self.lastNameView=[[UITextField alloc]init];
self.lastNameView.borderStyle=UITextBorderStyleRoundedRect;
self.lastNameView.text=self.detailFrommain.lastName;
[self.view addSubview:self.lastNameView];
[self.lastNameView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.top.equalTo(self.view).offset(baseoffset*2);
make.width.equalTo(self.firstNameView);
make.height.equalTo(self.firstNameView);
}];
self.phoneView=[[UITextField alloc]init];
self.phoneView.borderStyle=UITextBorderStyleRoundedRect;
self.phoneView.text=self.detailFrommain.phoneNumber;
[self.view addSubview:self.phoneView];
[self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.top.equalTo(self.view).offset(baseoffset*3);
make.width.equalTo(self.firstNameView);
make.height.equalTo(self.firstNameView);
}];
UIButton *backButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[backButton setTitle:@"確認" forState:UIControlStateNormal];
[self.view addSubview: backButton];
[backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[backButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.phoneView);
make.top.equalTo(self.phoneView.mas_bottom).offset(baseoffset);
}];
}
- (void)buttonAction:(id)sender {
if (self.callback) {
self.detailBackmain=[[KCContact alloc]init];
[self.detailBackmain setFirstName: self.firstNameView.text];
self.detailBackmain.lastName=self.lastNameView.text;
self.detailBackmain.phoneNumber=self.phoneView.text;
NSLog(@"%@",self.detailBackmain.firstName);
self.callback(self.selectedIndexPath, self.detailBackmain);
}
[self.navigationController popViewControllerAnimated: 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