如果插入了新项目,是否可以更改表格单元格的背景颜色。我可以更改所有单元格的背景颜色,但我只能更改新添加的单元格之一来更改颜色。
有什么办法可以做到这一点吗?
- (void)insertNewObject:(OutgoingHolder*)expense {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
Outgoing *outgoing = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
//[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
// Save the context.
outgoing.costDescription = expense.costDescription;
outgoing.amount = [NSNumber numberWithFloat:expense.amount];
outgoing.date = expense.date;
outgoing.category = expense.category;
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
//CHANGES HERE
self.addedNewRow=0;
[self.tableView reloadData];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return[sectionInfo name];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
if (_total > 0.0 ) {
//UPDATE 1:
NSLog(@"%@",[@"Total : " stringByAppendingString:[NSString stringWithFormat:@"%.2f", self.total]]);
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][indexPath.section];
if ([sectionInfo numberOfObjects]-1 == indexPath.row && self.addedNewRow!=-1) {
cell.contentView.backgroundColor = [UIColor orangeColor];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
}
}
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Outgoing *outgoing = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = outgoing.costDescription;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f",[outgoing.amount floatValue]];
}
最佳答案
可以通过以下方式获取新添加的单元格,前提是新单元格总是添加在最后一个位置。
只需在您的.h
文件中定义标志变量
@property (nonatomic, assign) int addedNewRow;
现在在你的 .m
文件中初始化 addedNewRow
为 -1
就像,
- (void)viewDidLoad {
[super viewDidLoad];
self.addedNewRow=-1;
//other stuff
}
替换下面两个对您的代码有一点增强的方法,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
//UPDATE 1:
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][indexPath.section];
if ([sectionInfo numberOfObjects]-1 == indexPath.row && self.addedNewRow!=-1) {
cell.contentView.backgroundColor = [UIColor orangeColor];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
}
return cell;
}
- (IBAction)insertNewElement:(UIBarButtonItem *)sender
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
Outgoing *outgoing = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
//[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
// Save the context.
outgoing.costDescription = expense.costDescription;
outgoing.amount = [NSNumber numberWithFloat:expense.amount];
outgoing.date = expense.date;
outgoing.category = expense.category;
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
//CHANGES HERE
self.addedNewRow=0;
[self.tableView reloadData];
}
希望对你有帮助!!
关于ios - 更改单元格的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29986013/