2011年10月27日

建立一個可以新增、移除Cell的Table

Photo 11-10-27 1 57 55

Photo 11-10-27 1 58 06

.h檔:

#import

@interface FirstViewController : UITableViewController
{
UITableView *table;
NSMutableArray *tableDataSource;
}

-(IBAction)addButtonPressed:(id)sender;

@end

.m檔:


#import "FirstViewController.h"

@implementation FirstViewController

- (IBAction)addButtonPressed:(id)sender
{
if(tableDataSource != nil)
{
[tableDataSource addObject:@"New City"];
}
[table reloadData];
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

tableDataSource = [[NSMutableArray alloc]init];
[tableDataSource addObjectsFromArray:[NSArray arrayWithObjects: @"Canada", @"United States", @"Australia", @"United Kingdom", nil]];

table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStyleGrouped];
[table setDataSource:self];
[table setDelegate:self];
[self.view addSubview:table];

[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];

}

- (void)viewDidUnload
{
[super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self tableView] reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableDataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [tableDataSource objectAtIndex:indexPath.row];

return cell;
}

#pragma - Enable/Disable Edit Button

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[table setEditing:editing animated:animated];

if(self.editing)
{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addButtonPressed:)];
[[self navigationItem] setRightBarButtonItem:addButton];
}
else
{
[[self navigationItem] setRightBarButtonItem:nil];
}

}

#pragma - Editing Style

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

[tableDataSource removeObjectAtIndex:indexPath.row];
[table reloadData];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{

}
}

#pragma - Can Move Row

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *cellItem = [tableDataSource objectAtIndex:fromIndexPath.row];
[tableDataSource removeObject:cellItem];
[tableDataSource insertObject:cellItem atIndex:toIndexPath.row];
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

#pragma mark - Table Title

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == 0)
{
return @"City";
}

else
{
return @"Nothing here";
}
}

#pragma mark - Table view Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
//TBD....
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

0 意見:

張貼意見