【问题标题】:Add text from UITextField to NSMutableArray and display it in UITableView将文本从 UITextField 添加到 NSMutableArray 并在 UITableView 中显示
【发布时间】:2012-03-03 06:02:02
【问题描述】:

我刚开始使用 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: 方法。

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

【问题讨论】:

    标签: objective-c uitableview nsmutablearray uitextfield


    【解决方案1】:

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

    【讨论】:

    • 好的。我查看了文档,但我究竟需要向我的项目添加什么?
    • 你需要一些对象,可能是持有itemsArray 的对象,来实现UITableViewDataSource 协议。如果你不熟悉 Objective-C 协议的工作原理,你可以阅读它们here
    • 所以我了解了协议的作用,但现在如何将它放入我的应用程序中?我知道这对您来说可能听起来很容易,但我才刚刚开始。我现在需要在项目中进行哪些更改?
    • 您需要更新将实现协议的对象的接口(在这种情况下是您的TableViewController 类)。它应该如下所示:@interface TableViewController : UITableViewController&lt;UITableViewDataSource&gt; {。完成此操作后,您需要实现协议的任何方法,例如返回表中行数的方法。请参阅docs for UITableViewDataSource 以了解您需要实现哪些方法。
    • 我没有得到关于 UITableViewDataSource 应该添加的内容
    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多