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;
[rootVC release];
[naVC release];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
RootViewController.m
#import "RootViewController.h"
#import "Student.h"
#import "MyCell.h"
#import "DetailViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *stuArr;
@end
@implementation RootViewController
- (void)dealloc
{
[_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:@"Students" ofType:@"plist"];
NSArray *arr = [NSArray arrayWithContentsOfFile:path];
// NSLog(@"%@", arr);
self.stuArr = [NSMutableArray array];
for (NSDictionary *dic in arr) {
Student *student = [[Student alloc] init];
[student setValuesForKeysWithDictionary:dic];
[self.stuArr addObject:student];
[student release];
// for (Student *stu in self.stuArr) {
// NSLog(@"~~~%@", stu.name);
// }
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor orangeColor];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
[self.tableView release];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.rowHeight = 100;
self.tableView.separatorStyle = NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailVC = [[DetailViewController alloc] init];
[self.navigationController pushViewController:detailVC animated:YES];
void (^block)(Student *) = ^(Student *dic) {
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
// [tableView reloadData];
};
detailVC.block = block;
detailVC.stu = self.stuArr[indexPath.row];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = @"reuse";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
}
Student *student = self.stuArr[indexPath.row];
cell.topLabel.text = student.name;
cell.downLabel.text = student.phone;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.stuArr.count;
}
DetailViewController.h
#import <UIKit/UIKit.h>
#import "Student.h"
typedef void (^Block)(Student *);
@interface DetailViewController : UIViewController
@property(nonatomic, copy)Block block;
@property(nonatomic, retain)Student *stu;
@end
DetailViewController.m
#import "DetailViewController.h"
@interface DetailViewController ()<UITextFieldDelegate>
@property(nonatomic, retain)NSArray *arr;
@end
@implementation DetailViewController
- (void)dealloc
{
[_arr release];
[_stu release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
NSArray *arr = @[@"name", @"phone", @"sex", @"address", @"hobby", @"age"];
NSMutableArray *studentArray = [NSMutableArray arrayWithObjects:self.stu.name, self.stu.phone, self.stu.sex, self.stu.address, self.stu.hobby, self.stu.age, nil];
for (NSInteger i = 0; i < 6; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100 + 50 * i, 70, 30)];
[self.view addSubview:label];
[label release];
label.text = [NSString stringWithFormat:@"%@%@", arr[i], @":"];
label.backgroundColor = [UIColor colorWithRed:208 / 255.0 green:208 / 255.0 blue:208 / 255.0 alpha:1];
label.textAlignment = NSTextAlignmentCenter;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(150, 100 + 50 * i, 150, 30)];
[self.view addSubview:textField];
[textField release];
textField.backgroundColor = [UIColor colorWithRed:208 / 255.0 green:208 / 255.0 blue:208 / 255.0 alpha:1];
textField.tag = i;
textField.text = studentArray[i];
textField.delegate = self;
// textField.background = [UIImage imageNamed:@"1.jpg"];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:button];
button.frame = CGRectMake(150, 430, 50, 30);
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)click:(UIButton *)button {
[self.navigationController popToRootViewControllerAnimated:YES];
// self.stu.name = @"阿拉擦";
// for (NSInteger i = 0; i < 6; i++) {
// UITextField *textField1 = (UITextField *)[self.view viewWithTag:i];
// NSLog(@"%@", textField1.text);
// }
// NSLog(@"%@", self.stu.name);
self.block(self.stu);
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
switch (textField.tag) {
case 0:
self.stu.name = textField.text;
break;
case 1:
self.stu.phone = textField.text;
break;
case 2:
self.stu.sex = textField.text;
break;
case 3:
self.stu.address = textField.text;
break;
case 4:
self.stu.hobby = textField.text;
break;
case 5:
self.stu.age = textField.text;
break;
default:
break;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
MyCell.h
#import <UIKit/UIKit.h>
@interface MyCell : UITableViewCell
@property(nonatomic, retain)UIView *bottomView;
@property(nonatomic, retain)UILabel *topLabel;
@property(nonatomic, retain)UILabel *downLabel;
@end
MyCell.m
#import "MyCell.h"
@implementation MyCell
- (void)dealloc
{
[_bottomView release];
[_topLabel release];
[_downLabel release];
[super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createView];
}
return self;
}
- (void)createView {
self.bottomView = [[UIView alloc] init];
[self.contentView addSubview:self.bottomView];
[_bottomView release];
self.bottomView.backgroundColor = [UIColor colorWithRed:208 / 255.0 green:208 / 255.0 blue:208 / 255.0 alpha:1];
self.bottomView.layer.cornerRadius = 10;
self.topLabel = [[UILabel alloc] init];
[self.bottomView addSubview:self.topLabel];
[_topLabel release];
// self.topLabel.layer.borderWidth = 1;
self.topLabel.layer.cornerRadius = 10;
self.downLabel = [[UILabel alloc] init];
[self.bottomView addSubview:self.downLabel];
[_downLabel release];
// self.downLabel.layer.borderWidth = 1;
self.downLabel.layer.cornerRadius = 10;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.bottomView.frame = CGRectMake(5, 5, self.contentView.frame.size.width - 10, self.contentView.frame.size.height - 10);
self.topLabel.frame = CGRectMake(5, 5, self.bottomView.frame.size.width - 10, self.bottomView.frame.size.height / 2 - 10);
self.downLabel.frame = CGRectMake(5, self.bottomView.frame.size.height / 2 + 5, self.bottomView.frame.size.width - 10, self.bottomView.frame.size.height / 2 - 10);
}
Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic, copy)NSString *name;
@property(nonatomic, copy)NSString *sex;
@property(nonatomic, copy)NSString *age;
@property(nonatomic, copy)NSString *address;
@property(nonatomic, copy)NSString *hobby;
@property(nonatomic, copy)NSString *phone;
@end
Student.m
#import "Student.h"
@implementation Student
- (void)dealloc
{
[_name release];
[_sex release];
[_age release];
[_address release];
[_hobby release];
[_phone release];
[super dealloc];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
@end