创建视图控制器

时间:2013-10-17 22:30:54

标签: ios objective-c uiviewcontroller

我正在使用故事板创建一个iphone应用程序。我基本上是关于Objective c和Xcode的新手。

我有一个类别列表,每次点击一个类别它应该打开一个tableView,所以我可以在该类别中添加一个项目。但是,不是为每个类别获取不同的tableView,而是复制所有类别和添加的项目的相同表。

如何为每个标签创建新表?

提前致谢!

以下是我添加类别

的内容
@interface ListViewController ()

@end

@implementation ListViewController{
    NSMutableArray *items;
}



@synthesize lists;



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

- (void)viewDidLoad
{
    [super viewDidLoad];

    items = [NSMutableArray arrayWithCapacity:20];
    List *item = [[List alloc] init];
    item.title = @"Grocery List";
    [items addObject:item];

    item = [[List alloc]init];
    item.title = @"Project List";
    [items addObject:item];

    item = [[List alloc] init];
    item.title = @"Events List";
    [items addObject:item];

    self.lists = items;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [self.lists count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    // Configure the cell...

    /*UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListsCell"];

    List *list = [self.lists objectAtIndex:indexPath.row];

    cell.textLabel.text = list.title;*/

    ListCell *cell = (ListCell *)[tableView dequeueReusableCellWithIdentifier:@"ListsCell"];

    List *list = [self.lists objectAtIndex:indexPath.row];

    cell.titleLabel.text = list.title;

    return cell;

}

//Add new list, new row will be added on the bottom and its data source must always be sync
-(void)addViewControllerSave:(AddViewController *)controller addList:(List *)list{

    [self.lists addObject:list];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.lists count] - 1 inSection:0];

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    [self dismissViewControllerAnimated:YES completion:nil];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if(editingStyle == UITableViewCellEditingStyleDelete){

            [self.lists removeObjectAtIndex:indexPath.row];

            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }   

   /* else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }*/   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation
*/

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    if([segue.identifier isEqualToString:@"AddList"]){
        UINavigationController *navigationController = segue.destinationViewController;

        AddViewController *addViewController = [[navigationController viewControllers] objectAtIndex:0];

        addViewController.delegate = self;
    }

    else if([segue.identifier isEqualToString:@"ViewItem"]){
        UINavigationController *nav = segue.destinationViewController;

        ItemViewController *itemViewController = [[nav viewControllers] objectAtIndex:0];

        itemViewController.delegate = self;
    }
}



#pragma mark - AddViewControllerDelegate
-(void)addViewControllerCancel:(AddViewController *)controller{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)addViewControllerSave:(AddViewController *)controller{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)itemViewControllerBack:(ItemViewController *)controller{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

以下是我添加项目的内容:

@interface ItemViewController ()

@end

@implementation ItemViewController{
    NSMutableArray *newItems;
}

@synthesize items;

@synthesize delegate;

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    newItems = [NSMutableArray arrayWithCapacity:20];

   Item *i = [[Item alloc]init];

    i.listItem = @"a";
    [newItems addObject:i];

    self.items = newItems;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Configure the cell...

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemsCell"];

    Item *item = [self.items objectAtIndex:indexPath.row];

    cell.textLabel.text = item.listItem;

    return cell;
}

-(void)addIteViewControllerSave:(AddItemViewController *)controller addItem:(Item *)item{

    [self.items addObject:item];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.items count] -1 inSection:0];

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    [self dismissViewControllerAnimated:YES completion:nil];

}



#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    if([segue.identifier isEqualToString:@"AddItem"]){
        UINavigationController *navigationController = segue.destinationViewController;

        AddItemViewController *addItemViewController = [[navigationController viewControllers]objectAtIndex:0];

        addItemViewController.itemDelegate = self;
    }
}

#pragma mark - AddItemViewControllerDelegate
-(void)addItemviewControllerCancel:(AddItemViewController *)controller{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)addIteViewControllerSave:(AddItemViewController *)controller{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - ItemViewControllerDelegate
-(IBAction)back:(id)sender{
    [self.delegate itemViewControllerBack:self];
}



@end

AddItemViewController

#import "AddItemViewController.h"
#import "Item.h"

@interface AddItemViewController ()


@end

@implementation AddItemViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(IBAction)cancel:(id)sender{
    [self.itemDelegate addItemviewControllerCancel:self];
}

-(IBAction)save:(id)sender{
    Item *item = [[Item alloc] init];

    item.listItem = self.listItemTextField.text;

    [self.itemDelegate addIteViewControllerSave:self addItem:item];
}

@end

1 个答案:

答案 0 :(得分:1)

不要为每个标签创建新的表格视图,而是使用每个标签的不同数据填充它,您需要更改存储数据的对象并调用

[tableView reloadData];

如果您需要清除阵列中所有添加的项目,则在添加其他内容之前用于存储对象。

[self.items removeAllObjects];