Core data UILabels未更新
我有一个由核心数据填充的Core data UILabels未更新,core-data,uitableview,reloaddata,Core Data,Uitableview,Reloaddata,我有一个由核心数据填充的UITableView。现在,当我点击一个单元格时,它会将我推到另一个视图,在那里我可以编辑特定索引中的数据,当我返回时,系统要么崩溃,要么不将更改加载到标签上,有什么想法吗?代码如下 -(void)viewWillAppear:(BOOL)animated { searchCriteria = [[NSMutableString alloc] initWithString:@"clientName"]; NSFetchRequest *fetc
UITableView
。现在,当我点击一个单元格时,它会将我推到另一个视图,在那里我可以编辑特定索引中的数据,当我返回时,系统要么崩溃,要么不将更改加载到标签上,有什么想法吗?代码如下
-(void)viewWillAppear:(BOOL)animated
{
searchCriteria = [[NSMutableString alloc] initWithString:@"clientName"];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"clientName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
[self.clientTableView reloadData];
[super viewWillAppear:animated];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Client * client = [self.fetchedResultsController objectAtIndexPath:indexPath];
clientNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 250.00, 30.0)];
clientNameLabel.tag = 1;
clientNameLabel.backgroundColor = [UIColor clearColor];
clientNameLabel.textColor = [UIColor whiteColor];
clientNameLabel.font = [UIFont boldSystemFontOfSize:13];
clientNameLabel.text = client.clientName;
[cell.contentView addSubview:clientNameLabel];
clientAccountNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 35.0, 250.00, 30.00)];
clientAccountNumberLabel.tag = 2;
clientAccountNumberLabel.textColor = [UIColor whiteColor];
clientAccountNumberLabel.backgroundColor = [UIColor clearColor];
clientAccountNumberLabel.font = [UIFont boldSystemFontOfSize:13];
clientAccountNumberLabel.text = client.clientAccountNumber;
[cell.contentView addSubview:clientAccountNumberLabel];
clientTelephoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(300.0, 0.0, 250, 30.00)];
clientTelephoneNumberLabel.tag = 3;
clientTelephoneNumberLabel.textColor = [UIColor whiteColor];
clientTelephoneNumberLabel.backgroundColor = [UIColor clearColor];
clientTelephoneNumberLabel.font = [UIFont boldSystemFontOfSize:13];
[cell.contentView addSubview:clientTelephoneNumberLabel];
addressLine1Label = [[UILabel alloc] initWithFrame:CGRectMake(315.0, 35.0, 250, 30.00)];
addressLine1Label.tag = 4;
addressLine1Label.textColor = [UIColor whiteColor];
addressLine1Label.backgroundColor = [UIColor clearColor];
addressLine1Label.font = [UIFont boldSystemFontOfSize:13];
addressLine1Label.text = client.addressLine1;
[cell.contentView addSubview:addressLine1Label];
addressLine2Label = [[UILabel alloc] initWithFrame:CGRectMake(315.0, 35.0, 250, 30.00)];
addressLine2Label.tag = 5;
addressLine2Label.textColor = [UIColor whiteColor];
addressLine2Label.backgroundColor = [UIColor clearColor];
addressLine2Label.text = client.addressLine2;
addressLine2Label.font = [UIFont boldSystemFontOfSize:13];
[cell.contentView addSubview:addressLine2Label];
addressLine3Label = [[UILabel alloc] initWithFrame:CGRectMake(315.0, 35.0, 250, 30.00)];
addressLine3Label.tag = 6;
addressLine3Label.textColor = [UIColor whiteColor];
addressLine3Label.backgroundColor = [UIColor clearColor];
addressLine3Label.font = [UIFont boldSystemFontOfSize:13];
addressLine3Label.text = client.addressLine3;
[cell.contentView addSubview:addressLine3Label];
addressLine4Label = [[UILabel alloc] initWithFrame:CGRectMake(315.0, 35.0, 250, 30.00)];
addressLine4Label.tag = 7;
addressLine4Label.textColor = [UIColor whiteColor];
addressLine4Label.backgroundColor = [UIColor clearColor];
addressLine4Label.font = [UIFont boldSystemFontOfSize:13];
addressLine4Label.text = client.addressLine4;
[cell.contentView addSubview:addressLine4Label];
return cell;
}
崩溃日志如下所示:
2012-06-23 17:08:05.541 iSalesForce[11773:15803] no object at index 1 in section at index 0
您可能会发现其他一些有用的代码有:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [fetchedObjects count];
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70.0;
}
问题很可能是您的fetchedResultsController已过时(您使用的是ARC吗?fetchedResultsController是“
strong
”属性吗
或者可能只是因为fetchedResultsController没有对象,而您坚持在“numberOfSectionsInTableView
”方法中只有一个对象,而不管“fetchedResultsController
”的真实内容如何
尝试此新功能和改进功能:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(cell == nil)
{
// this should never happen, but just in case...
NSLog( @"cell is still - unexpectedly - nil" );
} else {
if(self.fetchedResultsController == nil)
{
NSLog( @"surprise, fetchedResultsController is nil" );
} else {
Client * client = nil;
@try {
client = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
@catch (NSException * e) {
NSLog( @"exception thrown while trying to fetch something from fetchedResultsController - %@ %@", [e name], [e reason] );
}
@finally {
clientNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 250.00, 30.0)];
clientNameLabel.tag = 1;
clientNameLabel.backgroundColor = [UIColor clearColor];
clientNameLabel.textColor = [UIColor whiteColor];
clientNameLabel.font = [UIFont boldSystemFontOfSize:13];
clientNameLabel.text = client.clientName;
[cell.contentView addSubview:clientNameLabel];
clientAccountNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 35.0, 250.00, 30.00)];
clientAccountNumberLabel.tag = 2;
clientAccountNumberLabel.textColor = [UIColor whiteColor];
clientAccountNumberLabel.backgroundColor = [UIColor clearColor];
clientAccountNumberLabel.font = [UIFont boldSystemFontOfSize:13];
clientAccountNumberLabel.text = client.clientAccountNumber;
[cell.contentView addSubview:clientAccountNumberLabel];
clientTelephoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(300.0, 0.0, 250, 30.00)];
clientTelephoneNumberLabel.tag = 3;
clientTelephoneNumberLabel.textColor = [UIColor whiteColor];
clientTelephoneNumberLabel.backgroundColor = [UIColor clearColor];
clientTelephoneNumberLabel.font = [UIFont boldSystemFontOfSize:13];
[cell.contentView addSubview:clientTelephoneNumberLabel];
addressLine1Label = [[UILabel alloc] initWithFrame:CGRectMake(315.0, 35.0, 250, 30.00)];
addressLine1Label.tag = 4;
addressLine1Label.textColor = [UIColor whiteColor];
addressLine1Label.backgroundColor = [UIColor clearColor];
addressLine1Label.font = [UIFont boldSystemFontOfSize:13];
addressLine1Label.text = client.addressLine1;
[cell.contentView addSubview:addressLine1Label];
addressLine2Label = [[UILabel alloc] initWithFrame:CGRectMake(315.0, 35.0, 250, 30.00)];
addressLine2Label.tag = 5;
addressLine2Label.textColor = [UIColor whiteColor];
addressLine2Label.backgroundColor = [UIColor clearColor];
addressLine2Label.text = client.addressLine2;
addressLine2Label.font = [UIFont boldSystemFontOfSize:13];
[cell.contentView addSubview:addressLine2Label];
addressLine3Label = [[UILabel alloc] initWithFrame:CGRectMake(315.0, 35.0, 250, 30.00)];
addressLine3Label.tag = 6;
addressLine3Label.textColor = [UIColor whiteColor];
addressLine3Label.backgroundColor = [UIColor clearColor];
addressLine3Label.font = [UIFont boldSystemFontOfSize:13];
addressLine3Label.text = client.addressLine3;
[cell.contentView addSubview:addressLine3Label];
addressLine4Label = [[UILabel alloc] initWithFrame:CGRectMake(315.0, 35.0, 250, 30.00)];
addressLine4Label.tag = 7;
addressLine4Label.textColor = [UIColor whiteColor];
addressLine4Label.backgroundColor = [UIColor clearColor];
addressLine4Label.font = [UIFont boldSystemFontOfSize:13];
addressLine4Label.text = client.addressLine4;
[cell.contentView addSubview:addressLine4Label];
}
Client * client = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
}
return cell;
}
(我并没有实际测试过……但我确实为您添加了一些新的错误检查和异常处理)tl;dr--您实际遇到的崩溃是什么?显示回溯。也显示“
numberOfSectionsInTableView:
”方法的代码。不要将这些内容放入注释中,这对我(或其他人)来说都很难做到阅读。编辑您的原始问题,并加入这些附加方法和崩溃日志/回溯。但为什么会这样做,[FetchedObject count]有数据在里面,这让我很沮丧!好吧!!这很好地解决了,一件小事,为什么当我更新另一个视图控制器中的值,然后返回到这个视图时,数据没有改变?这没有意义,因为我在-(void)视图中有搜索代码将出现:(BOOL)动画方法可能是因为您没有明确告诉表“reloadData
”。可以将其放在父视图控制器的“视图中将出现:
”方法中。解决了该问题后,我将尽快发布自己的答案:Dstatic NSString*CellIdentifier=@“Cell”;UITableViewCell*Cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];是答案