我正在尝试做的是一个应用程序,它可以让你在一个字段中输入一个任务,按插入并在它下面的表格中查看它。我有我的表格 ( *table
)、我的字段 ( *taskField
)、我的按钮 ( *insert
) 和我的数组 ( *tasks
)。我运行应用程序,输入内容并按插入,但表格中没有显示任何内容。我也相信我所有的“IB”东西都设置好了。
这是我的代码:
NSString *docPath()
{
NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"data.td" ];
}
#import "CookViewController.h"
@interface CookViewController ()
@end
@implementation CookViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)addRec:(id)sender
{
NSString *t=[taskField text];
if ([t isEqualToString:@""]) {
return;
}
[tasks addObject:t];
[table reloadData];
[taskField setText:@""];
[taskField resignFirstResponder];
[tasks writeToFile:docPath()
atomically:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tasks removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; }
}
#pragma mark - Table View management
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tasks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *c= [table dequeueReusableCellWithIdentifier:@"Cell"];
if (!c) {
c= [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSString *item = [tasks objectAtIndex:[indexPath row]];
[[c textLabel] setText:item];
return c;
}
@end