您的位置:首页 > 产品设计 > UI/UE

UITableVIew读取plist文件及删除对象的操作

2012-06-16 12:15 393 查看
//  PlistAndTableViewViewController.m
//  PlistAndTableView
//
//  Created by Lixf on 09-10-24.
//  Copyright Lixf 2009. All rights reserved.
//
]#import "PlistAndTableViewViewController.h"
#define BARBUTTON(TITLE, SELECTOR)     [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]//UIBarButtonItem

@implementation PlistAndTableViewViewController
@synthesize Data, DataArray, delArray;
@synthesize DataTable;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Data List";
[self initData];

DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 320, 480) style:UITableViewStylePlain];
[DataTable setDelegate:self];
[DataTable setDataSource:self];
[self.view addSubview:DataTable];
[DataTable release];
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Edit", @selector(DeleteData:));
self.navigationItem.leftBarButtonItem = BARBUTTON(@"Revert", @selector(RevertData:));
}

//初始化数据:从plist文件中读取数据
-(void)initData{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSMutableArray *array = [[NSArray alloc] initWithContentsOfFile:path];
self.Data = array;
self.DataArray = array;
[array release];
}

//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [DataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = indexPath.row;
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SimpleTableIdentifier] autorelease];
}
cell.textLabel.text = [DataArray objectAtIndex:row];
return cell;
}

//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger row = indexPath.row;
NSLog(@"%@",[DataArray objectAtIndex:row]);
//[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}

//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}

//编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger row = indexPath.row;
[array addObjectsFromArray:DataArray];
[array removeObjectAtIndex:row];
self.DataArray = array;
[array release];
[DataTable reloadData];
}

//响应TableView编辑事件
-(void)DeleteData:(id)sender{
if(isClick == YES)
{
[DataTable beginUpdates];
[DataTable setEditing:YES animated:YES];
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Edit", @selector(DeleteData:));
isClick = NO;
}else {
[DataTable setEditing:NO animated:YES];
[DataTable endUpdates];
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Done", @selector(DeleteData:));
isClick = YES;
}

}

//还原数据
-(void)RevertData:(id)sender{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:Data];
self.DataArray = array;
[array release];
[DataTable setEditing:NO animated:YES];
[DataTable endUpdates];
[DataTable reloadData];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  path action list
相关文章推荐