//
// ViewController.m
// UI-NO-15
//
// Created by Bruce on 15/8/1.
// Copyright (c) 2015年 Bruce. All rights reserved.
//
#import "ViewController.h"
#import "ShowDetail_TableViewCell.h"
#define WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
UITableView *myTableView;
NSMutableArray *list;
UIView *editBgView;
NSString *nameString;
NSString *priceString;
NSString *contentString;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
[self loadData];
[self creatViews];
}
- (void)loadData
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths lastObject] stringByAppendingPathComponent:@"DetailData.plist"];
list = [[NSArray arrayWithContentsOfFile:path] mutableCopy];
if (myTableView) {
[myTableView reloadData];
}
}
- (void)creatViews
{
UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edit)];
self.navigationItem.leftBarButtonItem = left;
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(add)];
self.navigationItem.rightBarButtonItem = right;
// UITableViewStylePlain 表头不会跟随滚动视图一起滚动到最上面消失
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
myTableView.backgroundColor = [UIColor clearColor];
myTableView.delegate = self;
myTableView.dataSource = self;
// 设置分割线的样式
myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:myTableView];
}
- (void)edit
{
[myTableView setEditing:!myTableView.isEditing animated:YES];
}
- (void)add
{
[self editView];
}
- (void)editView
{
if (!editBgView) {
editBgView = [[UIView alloc]initWithFrame:self.view.frame];
editBgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:editBgView];
NSArray *buttonTitles = @[@"取消",@"完成"];
for (int i=0; i<2; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10+(CGRectGetWidth(editBgView.frame)-100)*i, CGRectGetHeight(editBgView.frame)-60, 80, 40);
[button setTitle:buttonTitles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 200+i;
[editBgView addSubview:button];
}
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100, WIDTH/3, 190-4)];
imageView.image = [UIImage imageNamed:@"1.jpg"];
[editBgView addSubview:imageView];
NSArray *titles = @[@"商品名",@"价格",@"介绍"];
for (int i=0; i<3; i++) {
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+5, 100+(35+5)*i, 200, 35)];
textField.delegate = self;
textField.tag = 100+i;
textField.placeholder = titles[i];
[editBgView addSubview:textField];
}
}
}
- (void)action:(UIButton *)sender
{
if (sender.tag == 200) {
if (editBgView) {
[editBgView removeFromSuperview];
editBgView = nil;
}
}else{
if (editBgView) {
if (nameString.length!=0&&priceString.length!=0&&contentString.length!=0) {
[editBgView removeFromSuperview];
editBgView = nil;
NSDictionary *dic = @{@"imageName":@"1.jpg",@"name":nameString,@"price":priceString,@"content":contentString};
NSMutableArray *newList = [list mutableCopy];
[newList addObject:dic];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths lastObject] stringByAppendingPathComponent:@"Data.plist"];
[newList writeToFile:path atomically:YES];
[self loadData];
}
}
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
switch (textField.tag) {
case 100:
nameString = textField.text;
break;
case 101:
priceString = textField.text;
break;
case 102:
contentString = textField.text;
break;
default:
break;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return list.count;
}
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1、有一个字符串类型的 cell的标识
NSString *cellID = @"detail";
// 2、查找tableView里面 有没有cell标识的cell存在
ShowDetail_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
// 3、如果没有查找到 创建cell
if (!cell) {
cell = [[ShowDetail_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell.backgroundColor = [UIColor clearColor];
// 设置cell的选中样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.showImageView.image = [UIImage imageNamed:list[indexPath.row][@"imageName"]];
cell.nameLabel.text = [NSString stringWithFormat:@"名字: %@",list[indexPath.row][@"name"]];
cell.priceLabel.text = [NSString stringWithFormat:@"价格: %@元/斤",list[indexPath.row][@"price"]];
cell.contentLabel.text = [NSString stringWithFormat:@"介绍: %@",list[indexPath.row][@"content"]];
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSDictionary *object = list[sourceIndexPath.row];
[list removeObjectAtIndex:sourceIndexPath.row];
[list insertObject:object atIndex:destinationIndexPath.row];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[list removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end