Reputation: 63
Is it possible to change the background colour of a table cell if a NEW item has been inserted. I am able to change the background colour for all the cells but however i just one the newly added ones to change colour.
Is there any way this can be done?
- (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]];
}
Upvotes: 0
Views: 156
Reputation: 3389
You can get the newly added cell with the following way, only if the new cell always added at last position.
Just define flag variable in your .h
file like
@property (nonatomic, assign) int addedNewRow;
Now in your .m
file initialise that addedNewRow
with -1
like,
- (void)viewDidLoad {
[super viewDidLoad];
self.addedNewRow=-1;
//other stuff
}
Replace below two method which has little bit enhancement from your code,
- (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];
}
Hope this helps you !!
Upvotes: 1
Reputation: 3733
You can applied validation in cellForRowAtIndexPath
for newly added cell.
in .h file declare one variable.
Outgoing *lastOutgoing;
As initial lastOutgoing = nil
in viewDidLoad
method. once new object is add. keep reference of that object in lastOutgoing
. here are the insertNewObject
modified method.
- (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();
}
lastOutgoing = outgoing;
[self.tableView reloadData];
}
In configureCell method
- (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]];
if([lastOutgoing isEqual:outgoing]){
cell.backgroundColor = [UIColor orangeColor];
}
}
Hope this help you..
Upvotes: 0
Reputation: 7903
Updated Answer:
numberOfRowsInSection method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
Within cellForRowAtIndexPath method:
if(indexPath.row == dataArray.count-1){
cell.contentView.backgroundColor = [UIColor blueColor];
}
Edit 1:
dataArray
is the array used to populate table rows, is the one you use to check the total number of elements in array to load in table within numberOfRowsInSection
method.
Upvotes: 0
Reputation: 319
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor whiteColor];
if (indexPath.row == dataArray.count-1)
{
[cell setBackgroundColor:[UIColor colorWithRed:123/255.0 green:234.0/255.0 blue:255/225.0 alpha:1.0]];
}
}
Upvotes: 0
Reputation: 2527
Change color using tableView: willDisplayCell:
method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (...){ //do your stuff.
[cell.backView setBckgroundColor:[UIColor redColor];
} else {
[cell.backView setBckgroundColor:[UIColor whiteColor];
}
}
change the background color of customize tableViewCell
Upvotes: 0
Reputation: 9397
Store the newly inserted indexPath
, and check for that indexPath in the willDisplayingCell:forRowAtIndexPath:
delegate method, and change the cell's contentView's background color.
Upvotes: 0
Reputation: 1477
Try this,
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0)
{
cell.backgroundColor =[UIColor blueColor];
} else {
cell.backgroundColor =[UIColor redColor];
}
}
Upvotes: 0