//
// ViewController.h
// uitableview 删除 添加 输入
//
// Created by 红珊瑚 on 15/5/12.
// Copyright (c) 2015年 红珊瑚. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(nonatomic,weak)UITableView *sdfbh;
@end
//
// ViewController.m
// uitableview 删除 添加 输入
//
// Created by 红珊瑚 on 15/5/12.
// Copyright (c) 2015年 红珊瑚. All rights reserved.
//
#import "ViewController.h"
#define kcheck @"check"
#define kimage @"image"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSArray *arrayold;
NSMutableArray *array1;
UITextField * TextField;
//NSMutableDictionary *MuDic;
UITableView *tableView;
NSIndexPath *indexPath;
UITableViewCell *cell;
NSMutableArray *listDataArr;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, 320, 568)];
tableView.backgroundColor=[UIColor groupTableViewBackgroundColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
arrayold = @[@"联系人",@"自己人",@"申明人",@"大好人",@"大坏人"];
array1 = [NSMutableArray array]; //array1=[NSMutableArray new];
[array1 addObjectsFromArray:arrayold];
TextField = [[UITextField alloc]initWithFrame:CGRectMake(110, 20, 100, 28)];
TextField.backgroundColor = [UIColor groupTableViewBackgroundColor];
TextField.placeholder = @"输入内容";
TextField.borderStyle = UITextBorderStyleRoundedRect;
TextField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.view addSubview:TextField];
/* UIBarButtonItem *AddButton = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(select1:)];
self.navigationItem.leftBarButtonItem = AddButton;
*/
UIButton *AddButton=[[UIButton alloc]initWithFrame:CGRectMake(20, 20, 60, 30)];
AddButton.backgroundColor = [UIColor groupTableViewBackgroundColor];
[AddButton setTitle:@"添加" forState:UIControlStateNormal];
[AddButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[AddButton addTarget:self action:@selector(select1:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:AddButton];
UIButton *DeleteButton=[[UIButton alloc]initWithFrame:CGRectMake(240, 20, 60, 30)];
DeleteButton.backgroundColor = [UIColor groupTableViewBackgroundColor];
[DeleteButton setTitle:@"删除" forState:UIControlStateNormal];
[DeleteButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[DeleteButton addTarget:self action:@selector(select2:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:DeleteButton];
/*
[arrayold enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{MuDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:arrayold,@"itme",@NO,@"isSelet", nil];}];
*/
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView // 组数
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section // 组.条数
{
return array1.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath // 显示字和图片
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
//cell.imageView.image=[UIImage imageNamed:@"check.png"];
cell.imageView.image = [UIImage imageNamed:@"uncheck.png"];
/*
if (indexPath.row == 0) {
cell.imageView.image = [UIImage imageNamed:@"check.png"];
cell.imageView.image = [UIImage imageNamed:@"uncheck.png"];
}
else if (indexPath.row == 1){
cell.imageView.image = [UIImage imageNamed:@"check.png"];
cell.imageView.image = [UIImage imageNamed:@"uncheck.png"];
}
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"check.png"]];
cell.accessoryView = imageView;
unimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"uncheck.png"]];
cell.accessoryView = unimageView;
UIImageView *IV = (UIImageView *)[cell.contentView viewWithTag:1];
IV.image = [UIImage imageNamed:@"check.png"];
[[cell textLabel] setText:[array objectAtIndex:indexPath.row]];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 200, 50)];
label.backgroundColor = [UIColor groupTableViewBackgroundColor];
label.text = [NSString stringWithFormat:@"%@",array];
[cell.contentView addSubview:label];
*/
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath//改变间距
{
if (indexPath.row==0) {
return 80;
}
return 40;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"uncheck.png"]])
{
cell.imageView.image=[UIImage imageNamed:@"check.png"];
}else{
cell.imageView.image=[UIImage imageNamed:@"uncheck.png"];
}
// if (cell.accessoryType == UITableViewCellAccessoryNone)
// {
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// cell.imageView.image=[UIImage imageNamed:@"check.png"];
//
// }else{
// cell.accessoryType = UITableViewCellAccessoryNone;
// cell.imageView.image=[UIImage imageNamed:@"uncheck.png"];
// }
//[tableView deselectRowAtIndexPath:indexPath animated:YES];
/*
UITableViewCell *cell2=[tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *array2= [NSMutableArray array];
array2 =
NSUInteger row = [indexPath row];
NSMutableDictionary *itemDic = array2[row];
BOOL isSel = [itemDic[@"isSelet"] boolValue];
itemDic[@"isSelet"] = [NSString stringWithFormat:@"%d",!isSel];
UIImageView *iv = (UIImageView *)[cell2.contentView viewWithTag:1];
iv.image = [itemDic[@"isSelet"] boolValue] ? [UIImage imageNamed:@"check.png"]:[UIImage imageNamed:@"uncheck.png"];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
*/ //抄的代码,但是不懂
/*
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
*/ //系统标记
/*
if (indexPath.section == 0)
{
NSIndexPath *indexPath = [tableView indexPathForSelectedRow]; //1
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //2
cell.imageView.image=[UIImage imageNamed:@"check.png"]; //3
cell.selected = NO;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
*/ //只显示 红 图片
}
-(void)select1:(id)sender
{
indexPath = [NSIndexPath indexPathForRow:array1.count inSection:0];
[array1 insertObject:TextField.text atIndex:array1.count]; // 输入并添加内容到cell
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//[tableView reloadData];
TextField.text = nil;
}
-(void)select2:(id)sender
{
// indexPath = [tableView indexPathForSelectedRow]; // 为 已经选择的cell,索引路径
// NSUInteger row = [indexPath row];
for (int i = (int)array1.count-1; i >= 0; i--)
{
NSIndexPath *indx=[NSIndexPath indexPathForRow:i inSection:0];//创建新的indexPath来制定它的遍历
UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indx];
if ([cell1.imageView.image isEqual:[UIImage imageNamed:@"check.png"]])
{
[array1 removeObjectAtIndex:i];
[tableView deleteRowsAtIndexPaths:@[indx] withRowAnimation:UITableViewRowAnimationFade];
}
}
[tableView reloadData];
//[array1 removeAllObjects];
// NSUInteger row = [indexPath row];
//
// [array1 removeObjectAtIndex:row];
// [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
// withRowAnimation:UITableViewRowAnimationAutomatic];
// [tableView beginUpdates];
// NSArray *removeRows = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]];
// [tableView deleteRowsAtIndexPaths:removeRows withRowAnimation:UITableViewRowAnimationNone];
// [tableView endUpdates];
// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:array1.count inSection:0];
// [array1 removeObjectAtIndex:array1.count];
// [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
/*
UITableView *tableView;
for (int i = (int)array1.count - 1; i >= 0; i--)
{
//NSMutableDictionary *itemo = array1[i];
if ([UIImage imageNamed:@"check.png"])
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[array1 removeObjectAtIndex:array1.count];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
*/
}