-1

我正在尝试做的是一个应用程序,它可以让你在一个字段中输入一个任务,按插入并在它下面的表格中查看它。我有我的表格 ( *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
4

1 回答 1

2

IB 清单:

右键单击表格视图并查看代表和参考出口:

  1. datasource指向你的ViewController
  2. delegate指向你的ViewController
  3. 引用出口已正确定义,并且ViewController标题中的变量旁边有一个黑色的小 O 以显示出口。

要设置datasourceand delegate,请按 CTRL 按钮并将其拖动到您的 ViewController。

您的 ViewController 在 ViewController 的头文件中有 UITableViewDataSource 和 UITableViewDelegate :

@interface ViewController:UIViewController<UITableViewDataSource, UITableViewDelegate>

注意:ViewController 在您的情况下是指 CookViewController。

编辑:更改此功能:

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



--V

于 2012-06-18T03:44:35.780 回答