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

来源:互联网 发布:社交媒体数据可视化 编辑:程序博客网 时间:2024/06/05 03:56
//  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)editingStyleforRowAtIndexPath:(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];}