2012-06-18 31 views
-1

我正在尝试做的是一个应用程序,它可以让你输入一个任务到一个字段,按下插入并在下面的表格中看到它。我有我的表格(*table),我的字段(*taskField),我的按钮(*insert)和我的阵列(*tasks)。我运行应用程序,键入内容并按下插入,但没有显示在表格中。我也相信我的所有“IB”的东西都设置正确。UITableView SDK添加阵列

这里是我的代码:

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 

回答

2

IB清单:

右键单击表视图,看到了代表和参考网点:

  1. datasource点使你ViewController
  2. delegate指向你的ViewController
  3. 参考插座已正确定义,并且ViewController标头中的变量旁边有一个小的变黑O以显示插座。

要设置datasourcedelegate,请按下CTRL按钮并将其拖到您的ViewController。

您的视图控制器具有UITableViewDataSource和的UITableViewDelegate在ViewController中的头文件:

@interface ViewController:UIViewController<UITableViewDataSource, UITableViewDelegate>

注意:通过视图控制器我的意思是CookViewController你的情况。

编辑:更改此功能:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    table.delegate = self; 
    table.dataSource = self; 
} 



--V

+0

我的数据源和委托没有指向任何东西,只要我可以告诉(邻的是空的) 。我的参考出口指向addRec并且有一个黑色的o。我没有在我的文件中看到任何像UITableViewDataSource。我需要改变一些东西吗?我的数据源/代表应该指向什么?我可以给你更多的信息吗?谢谢! – user1438042

+0

我已经用你需要做的两件事更新了我的答案,基本上将它们设置为指向StoryBoard中的ViewController并更改头文件。另外,你可以在'cellForRowAtIndexPath'函数中使用NSLog来知道它实际上是否被调用。 – vellvisher

+0

你是什么意思把他们拖到ViewController?在我的.h/.m文件中有一个?我不知道我需要拖到哪里。“@interface ViewController:UIViewController ”不显示在我的文件中的任何位置。什么是StoryBoard?感谢您的帮助,对不起,如果我很难,我很新。 – user1438042