1

我正在使用情节提要创建一个 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

添加项目视图控制器

#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
4

1 回答 1

1

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

[tableView reloadData];

如果所有添加的项目您可能需要在添加其他内容之前清除用于存储对象的数组。

[self.items removeAllObjects];
于 2013-10-17T22:38:47.403 回答