2015-04-07 159 views
-1

我使用core data作爲我的數據庫。在我的UITableView裏面,我有一個按鈕來添加項目,但在這一刻我只用它來添加名稱。但我添加的名稱不會顯示在桌面上。 我認爲我的問題是我需要重新填充我的projectArray,我該如何以及在哪裏做。 這裏是我的代碼:TableView不顯示單元格內容

- (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]; 
} 
+0

你在哪裏設置tableView的數據源和委託? – vikingosegundo

+0

也應該從提取請求中評估錯誤對象。 – vikingosegundo

回答

0

你有沒有設置DataSource並委託tableView的屬性?

self.tableView.delegate = self; 
self.tableView.dataSource = self; 
+0

是的,我有,但它對結果沒有任何影響 – 000

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"] 
} 
+0

它沒有聲明的數組的接口setObject – 000

+0

聲明你的數組爲NSMtableArray並初始化它 – Shruti

+0

好吧,我已經改變它爲一個可變數組,你能幫我從那裏? – 000

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; 
} 

它應該做的魔力......

相關問題