如果插入了新项目,是否可以更改表格单元格的背景颜色。我可以更改所有单元格的背景颜色,但是我只添加了一个更改颜色的颜色。
有什么办法可以做到吗?
- (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]];
}
答案 0 :(得分:1)
只有新单元格总是在最后位置添加时,才能通过以下方式获取新添加的单元格。
只需在.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];
}
希望这可以帮助你!!
答案 1 :(得分:0)
更新的答案:
numberOfRowsInSection方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
在cellForRowAtIndexPath方法中:
if(indexPath.row == dataArray.count-1){
cell.contentView.backgroundColor = [UIColor blueColor];
}
编辑1:
dataArray
是用于填充表行的数组,是用于检查要在numberOfRowsInSection
方法中加载到表中的数组中元素总数的数组。
答案 2 :(得分:0)
您可以在cellForRowAtIndexPath
中为新添加的单元格应用验证。
Outgoing *lastOutgoing;
作为lastOutgoing = nil
方法中的初始viewDidLoad
。一旦添加了新对象。在lastOutgoing
中继续引用该对象。这是insertNewObject
修改过的方法。
- (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];
}
在configureCell方法
中- (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];
}
}
希望这能帮到你..
答案 3 :(得分:0)
试试这个,
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0)
{
cell.backgroundColor =[UIColor blueColor];
} else {
cell.backgroundColor =[UIColor redColor];
}
}
答案 4 :(得分:0)
存储新插入的indexPath
,并在willDisplayingCell:forRowAtIndexPath:
委托方法中检查该indexPath,并更改单元格内容视图的背景颜色。
答案 5 :(得分:0)
使用tableView: willDisplayCell:
方法
- (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];
}
}
答案 6 :(得分:0)
- (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]];
}
}