将UITextField中的文本添加到NSMutableArray并在UITableView中显示它

时间:2012-03-03 06:02:02

标签: objective-c uitableview nsmutablearray uitextfield

我刚开始使用Objective-C来编写iPhone应用程序。我读了几本书并用书做了一些项目,但我还不是专家。我试图从UITextField添加一些文本到NSMutableArray,然后在UITableView中显示它。我知道这对你们大多数人来说都是非常基本的东西,但我只是一个初学者。到目前为止,我有两个文件: TableViewController.h:

#import <UIKit/UIKit.h>
@class AddItemViewController;

@interface TableViewController : UITableViewController {
    AddItemViewController *addItemViewController;
    NSMutableArray *itemsArray;

}
-(IBAction)addNewItem:(id)sender;
@property (nonatomic, retain)NSMutableArray *itemsArray;
@end

TableViewController.m

#import "TableViewController.h"
#import "AddItemViewController.h"


@implementation TableViewController
@synthesize itemsArray;

-(id)init
{
    [super initWithStyle:UITableViewStyleGrouped];

itemsArray = [[NSMutableArray alloc] init];

[self setTitle:@"Playing Around"];

UIBarButtonItem *bbi = [[UIBarButtonItem alloc]
                        initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem:)];
[[self navigationItem] setLeftBarButtonItem:bbi];
[bbi release];
return self;
}

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

#pragma mark Actions

-(IBAction)addNewItem:(id)sender
{
addItemViewController  = [[AddItemViewController alloc] init];
[UIView beginAnimations:@"animations" context:nil];
[[self navigationController] pushViewController:addItemViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
[UIView setAnimationDuration:0.7];
[UIView commitAnimations];


}

#pragma mark TableView Things

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

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    [cell autorelease];
}
cell.textLabel.text = [itemsArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;

}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[itemsArray release];
[super dealloc];
}


@end

AddItemViewController.h

#import <UIKit/UIKit.h>
@class TableViewController;


@interface AddItemViewController : UIViewController {
TableViewController *tableViewController;
IBOutlet UITextField *textField;
NSString *value;

}
-(IBAction)cancel:(id)sender;
-(IBAction)create:(id)sender;
@property (nonatomic, copy) NSString *value;
@property (nonatomic, retain)NSMutableArray *itemsArray;

@end

AddItemViewController.m

#import "AddItemViewController.h"
#import "TableViewController.h"


@implementation AddItemViewController
@synthesize value, itemsArray;

-(id)init
{
[self setTitle:@"Add Item"];
UIBarButtonItem *item;
item = [[UIBarButtonItem alloc]
                         initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
[[self navigationItem] setLeftBarButtonItem:item];
[item release];
item = [[UIBarButtonItem alloc]
                         initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(create:)];
[[self navigationItem] setRightBarButtonItem:item];
[item release];
return self;
}

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

-(void)viewDidUnload:(BOOL)animated
{
[super viewDidUnload];
[textField release];
textField = nil;
}

#pragma mark Actions

-(IBAction)cancel:(id)sender
{
tableViewController = [[TableViewController alloc] init];
[UIView beginAnimations:@"animations" context:nil];
[[self navigationController] pushViewController:tableViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView setAnimationDuration:0.7];
[UIView commitAnimations];


}

-(IBAction)create:(id)sender
{
    //This is where I get lost
NSString *string = [textField text];
NSLog(@"The string is: %@", string);
[itemsArray addObject:string];
[[tableViewController tableView] reloadData];

NSLog(@"The items are: %@", itemsArray);

tableViewController = [[TableViewController alloc] init];
[UIView beginAnimations:@"animations" context:nil];
[[self navigationController] pushViewController:tableViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView setAnimationDuration:0.7];
[UIView commitAnimations];
}

- (void)dealloc {
[textField release];
[value release];
[super dealloc];
}


@end

(对不起,我把整件事放在这里,但据我所知,我可能在这方面做错了。) 所以,如果你想跳过大部分的东西,我认为我做得对,只需转到create:方法。

现在该怎么办?我做到了这一点,但当我尝试将其添加到itemsArray时,UITableView中的文本将不会显示。任何提示都会很棒!提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的UITableView需要数据源。您的表视图控制器应实现UITableViewDataSource协议。 Here are the docs