TableView不显示单元格内容

时间:2015-04-07 09:57:12

标签: ios objective-c arrays uitableview core-data

我使用core data作为我的数据库。在我的UITableView里面,我有一个添加项目的按钮,但此时我只用它来添加名字。但我添加的名称不会显示在tableview上。 我认为我的问题是我需要重新填充我的projectArray,我该怎样以及在哪里这样做。 这是我的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [_projectArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    Project* project = _projectArray[indexPath.row];
    static NSString *cellID = @"TableCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.textLabel.text = project.name;
    cell.detailTextLabel.text = @"prooo";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    return cell;

}

这是我的按钮代码:

- (IBAction)addProjectButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter new project name."
                                                message:nil
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Ok", nil];

alert.alertViewStyle = UIAlertViewStylePlainTextInput;

[alert show];

UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = @"Project Name";


}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != alertView.cancelButtonIndex) {
    UITextField *field = [alertView textFieldAtIndex:0];
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = [delegate managedObjectContext];
    NSManagedObject *object =[NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:context];
    [object setValue:field.text forKey:@"name"];

    NSError *error;
    [context save:&error];
    if (![context save:&error]) {
        NSLog(@"Whoops %@ %@", error, [error localizedDescription]);
    }

    [self.tableView reloadData];

} else {

    NSLog(@"cancel");
}
}

这是我的ViewWillAppear代码,我在那里获取信息:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:nil];

AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = [delegate managedObjectContext];

//load project
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
[fetch setEntity:entity];
NSError *error;
_projectArray = [context executeFetchRequest:fetch error:&error];
}

3 个答案:

答案 0 :(得分:1)

你可以做很多事情。为了使它成为一个好的代码,我建议编写一个重新加载数组的方法。喜欢这个

-(void) reloadArray
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = [delegate managedObjectContext];

    //load project
    NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
    [fetch setEntity:entity];
    NSError *error;
    _projectArray = [context executeFetchRequest:fetch error:&error];
}

然后像这样替换你的viewDidAppear -

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:nil];

    [self reloadArray];
}

同样在您的numberOfSectionsInTableView:方法中,进行此更改 -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    [self reloadArray];
    return 1;
}

它应该做魔术......

答案 1 :(得分:0)

您是否设置了tableView的dataSource和delegate属性?

self.tableView.delegate = self;
self.tableView.dataSource = self;

答案 2 :(得分:0)

改变这个:

 NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
[fetch setEntity:entity];
NSError *error;
_projectArray = [context executeFetchRequest:fetch error:&error];

要:

 _projectArray = [[NSMutableArray alloc]init];
 NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
[fetch setEntity:entity];
NSManagedObject *object = nil;
NSError *error;
NSArray *result = [context executeFetchRequest:fetch error:&error];

 for (int i = 0; i <  [result count]; i ++) {
 object = [result objectAtIndex:i];
 [_projectArray setObject:[object valueForKey:@"name"]
}