UI09_自定义cell写通讯录

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc{
    [_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];

    [_window release];
    RootViewController *rootVC = [[RootViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
    self.window.rootViewController = naVC;
    [naVC release];
    [rootVC release];




    return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#import "RootViewController.h"
#import "FemaleCell.h"
#import "MaleCell.h"
#import "DetailViewController.h"
#import "AddViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate, ModifyVlaueDelegate, AddDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *stuArr;

@end

@implementation RootViewController

- (void)dealloc
{
    [_stuArr release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self createData];
    }
    return self;
}
- (void)createData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"0825Student" ofType:@"plist"];
    self.stuArr = [NSMutableArray array];
    self.stuArr = [NSMutableArray arrayWithContentsOfFile:path];

//    NSLog(@"%@", self.stuArr);
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add.png"] style:UIBarButtonItemStyleDone target:self action:@selector(addAction:)];


    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    self.tableView.rowHeight = 77;
    self.tableView.separatorStyle = NO;

    self.tableView.dataSource = self;
    self.tableView.delegate = self;




}

- (void)addAction:(id)sender {
    AddViewController *addVC = [[AddViewController alloc] init];
    [self.navigationController pushViewController:addVC animated:YES];
    [addVC release];

    addVC.delegate = self;
}
- (void)addNewMember:(NSMutableDictionary *)dic {
//    NSLog(@"%@", dic);
    [self.stuArr addObject:dic];
//    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];

    detailVC.tempDic = self.stuArr[indexPath.row];
    detailVC.delegate = self;
}
- (void)modifyTakeVale:(NSMutableDictionary *)dic {
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if ([self.stuArr[indexPath.row][@"sex"] isEqualToString:@"女"]) {
            static NSString *reuse = @"reuse";
            FemaleCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
            if (!cell) {
                cell = [[[FemaleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
            }
            cell.nameLabel.text = self.stuArr[indexPath.row][@"name"];
            cell.phoneLabel.text = self.stuArr[indexPath.row][@"phone"];
            cell.addressLabel.text = self.stuArr[indexPath.row][@"address"];
            return cell;
        }
        else {
            static NSString *maleReuse = @"maleReuse";
            MaleCell *cell = [tableView dequeueReusableCellWithIdentifier:maleReuse];
            if (!cell) {
                cell = [[[MaleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:maleReuse] autorelease];
            }
            cell.maleNameLabel.text = self.stuArr[indexPath.row][@"name"];
            cell.malePhoneLabel.text = self.stuArr[indexPath.row][@"phone"];
            return cell;
        }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.stuArr.count;
}

AddViewController.h

#import <UIKit/UIKit.h>

@protocol AddDelegate <NSObject>

- (void)addNewMember:(NSMutableDictionary *)dic;

@end

@interface AddViewController : UIViewController
@property(nonatomic, assign)id<AddDelegate>delegate;

@property(nonatomic, retain)NSMutableDictionary *dic;

@end

AddViewController.m

#import "AddViewController.h"

@interface AddViewController ()<UITextFieldDelegate>
@property(nonatomic, retain)NSMutableArray *array;

@end

@implementation AddViewController
- (void)dealloc
{
    [_dic release];
    [_array release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = [NSMutableArray arrayWithObjects:@"name", @"sex", @"phone", @"address", @"age", @"hobby", nil];

    for (NSInteger i = 0; i < 6; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 20 + 50 * i, 100, 30)];
        [self.view addSubview:label];
        [label release];
        label.layer.borderWidth = 1;
        label.text = self.array[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = i;

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(150, 20 + 50 * i, 100, 30)];
        [self.view addSubview:textField];
        [textField release];
        textField.layer.borderWidth = 1;
        textField.delegate = self;
        textField.tag = i + 6;

//        if (i ==  0) {
//            [textField  becomeFirstResponder];
//        }
    }


    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:button];
    button.frame = CGRectMake(180, 310, 50, 30);
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 10;
    button.layer.borderColor = [UIColor cyanColor].CGColor;
    [button setTitle:@"return" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(ReturnButton:) forControlEvents:UIControlEventTouchUpInside];


}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

}
//- (void)ReturnButton:(UIButton *)button {
//    NSLog(@"%@", self.dic);
    [self.navigationController popToRootViewControllerAnimated:YES];
//    [self.delegate addNewMember:self.dic];
//}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    UILabel *label = [[UILabel alloc] init];
    label = (UILabel *)[self.view viewWithTag:textField.tag - 6];
    [self.dic setObject:textField.text forKey:label.text];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];

//    UITextField *anotherTextField = (UITextField *)[self.view viewWithTag:++textField.tag];
//    [anotherTextField becomeFirstResponder];

//    switch (textField.tag) {
//        case 6:
//            <#statements#>
//            break;
//            
//        default:
//            break;
//    }




    return YES;
}

DetailViewController.h

#import <UIKit/UIKit.h>

@protocol ModifyVlaueDelegate <NSObject>

- (void)modifyTakeVale:(NSMutableDictionary *)dic;

@end

@interface DetailViewController : UIViewController
@property(nonatomic, assign)id<ModifyVlaueDelegate>delegate;

@property(nonatomic, retain)NSMutableDictionary *tempDic;

@end

DetailViewController.m

#import "DetailViewController.h"

@interface DetailViewController ()<UITextFieldDelegate>
@property(nonatomic, retain)NSArray *array;
@end

@implementation DetailViewController
- (void)dealloc
{
    [_tempDic release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = @[@"name", @"phone", @"age", @"address", @"hobby", @"sex"];
    for (NSInteger i = 0; i < 6; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10 + 30 * i, 120, 30)];
        [self.view addSubview:label];
        [label release];
        label.layer.borderWidth = 1;
        label.text = self.array[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = i;

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(150, 10 + 30 * i, 120, 30)];
        [self.view addSubview:textField];
        [textField release];
        textField.layer.borderWidth = 1;
        textField.text = self.tempDic[self.array[i]];
        textField.tag = i + 6;
        textField.delegate = self;

    }
    UIButton *modifyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:modifyButton];
    modifyButton.frame = CGRectMake(150, 530, 50, 30);
    [modifyButton setTitle:@"修改" forState:UIControlStateNormal];
    [modifyButton addTarget:self action:@selector(modifyAction:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:returnButton];
    returnButton.frame = CGRectMake(210, 530, 50, 30);
    [returnButton setTitle:@"返回" forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(returnAction:) forControlEvents:UIControlEventTouchUpInside];


}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self.tempDic setObject:textField.text forKey:self.array[textField.tag - 6]];
}

- (void)returnAction:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)modifyAction:(UIButton *)button {

//    [self.tempDic setObject:@"阿拉擦" forKey:@"name"];
    [self.delegate modifyTakeVale:self.tempDic];
}

FemaleCell.h

#import <UIKit/UIKit.h>

@interface FemaleCell : UITableViewCell

@property(nonatomic, retain)UIView *view;
@property(nonatomic, retain)UIImageView *femaleImageView;
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UILabel *phoneLabel;
@property(nonatomic, retain)UILabel *addressLabel;

@end

FemaleCell.m

#import "FemaleCell.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation FemaleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    self.view = [[UIView alloc] init];
    [self.contentView addSubview:self.view];
    [self.view release];
    self.view.backgroundColor = [UIColor lightGrayColor];

    self.femaleImageView = [[UIImageView alloc] init];
    [self.view addSubview:self.femaleImageView];
    [self.femaleImageView release];
    self.femaleImageView.backgroundColor = [UIColor orangeColor];

    self.nameLabel = [[UILabel alloc] init];
    [self.view addSubview:self.nameLabel];
    [self.nameLabel release];
    self.nameLabel.backgroundColor = [UIColor cyanColor];

    self.phoneLabel = [[UILabel alloc] init];
    [self.view addSubview:self.phoneLabel];
    [self.phoneLabel release];
    self.phoneLabel.backgroundColor = [UIColor yellowColor];

    self.addressLabel = [[UILabel alloc] init];
    [self.view addSubview:self.addressLabel];
    [self.addressLabel release];
    self.addressLabel.backgroundColor = [UIColor greenColor];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.femaleImageView.frame = CGRectMake(0, 0, WIDTH / 3, HEIGHT);
    self.nameLabel.frame = CGRectMake(WIDTH / 2, 0, WIDTH / 3, HEIGHT / 3);
    self.phoneLabel.frame = CGRectMake(WIDTH / 2, HEIGHT / 3, WIDTH / 3, HEIGHT / 3);
    self.addressLabel.frame = CGRectMake(WIDTH / 2, HEIGHT / 3 * 2, WIDTH / 3, HEIGHT / 3);
}

MaleCell.h

#import <UIKit/UIKit.h>

@interface MaleCell : UITableViewCell

@property(nonatomic, retain)UIView *view;
@property(nonatomic, retain)UILabel *maleNameLabel;
@property(nonatomic, retain)UILabel *malePhoneLabel;
@property(nonatomic, retain)UIImageView *leftImageView;
@property(nonatomic, retain)UIImageView *rightImageview;

@end

MaleCell.m

#import "MaleCell.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation MaleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    self.view = [[UIView alloc] init];
    [self.contentView addSubview:self.view];
    [self.view release];
    self.view.backgroundColor = [UIColor darkGrayColor];

    self.maleNameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.maleNameLabel];
    [self.maleNameLabel release];
    self.maleNameLabel.backgroundColor = [UIColor cyanColor];

    self.malePhoneLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.malePhoneLabel];
    [self.malePhoneLabel release];
    self.malePhoneLabel.backgroundColor = [UIColor greenColor];

    self.leftImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.leftImageView];
    [self.leftImageView release];
    self.leftImageView.backgroundColor = [UIColor lightGrayColor];

    self.rightImageview = [[UIImageView alloc] init];
    [self.contentView addSubview: self.rightImageview];
    [self.rightImageview release];
    self.rightImageview.backgroundColor = [UIColor grayColor];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.maleNameLabel.frame = CGRectMake(WIDTH / 9, 0, WIDTH / 3, HEIGHT / 3);
    self.malePhoneLabel.frame = CGRectMake(WIDTH / 9 * 5, 0, WIDTH / 3, HEIGHT / 3);
    self.leftImageView.frame = CGRectMake(WIDTH / 9, HEIGHT / 3, WIDTH / 3, HEIGHT / 3 * 2);
    self.rightImageview.frame = CGRectMake(WIDTH / 9 * 5, HEIGHT / 3, WIDTH / 3, HEIGHT / 3 * 2);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值