更改单元格的背景颜色
Posted
技术标签:
【中文标题】更改单元格的背景颜色【英文标题】:Change background colour of Cell 【发布时间】:2015-05-01 11:26:19 【问题描述】:如果插入了新项目,是否可以更改表格单元格的背景颜色。我可以更改所有单元格的背景颜色,但我只是新添加的一个来更改颜色。
有什么办法可以做到吗?
- (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]];
【问题讨论】:
有什么你试过的,可以给我们看看吗? 您可以更改每个单元格的背景颜色,您不仅限于all。 贴出你尝试过的代码 如何添加新单元格?你也可以发布那个代码吗? @ViralSavaj 请查看已编辑的代码问题 【参考方案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];
希望对你有帮助!!
【讨论】:
我收到关于 id试试这个,
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
if (indexPath.row % 2 == 0)
cell.backgroundColor =[UIColor blueColor];
else
cell.backgroundColor =[UIColor redColor];
【讨论】:
你确定这会工作,只有新添加的单元格?【参考方案3】:存储新插入的indexPath
,并在willDisplayingCell:forRowAtIndexPath:
委托方法中检查该indexPath,并更改单元格的contentView的背景颜色。
【讨论】:
【参考方案4】:使用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];
change the background color of customize tableViewCell
【讨论】:
【参考方案5】:- (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]];
【讨论】:
【参考方案6】:更新答案:
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
方法中加载到表格中。
【讨论】:
dataArray 的用途是什么? - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; [self configureCell:cell atIndexPath:indexPath]; if(indexPath.row == dataArray.count-1) cell.contentView.backgroundColor = [UIColor blueColor]; 返回单元格; dataArray 是用于馈送表格行的数组。 我对表格视图相当陌生,我在 dataArray 上收到一个错误,我不完全确定我的数组是什么。它会在哪里声明? 问题已更新!最好发布有问题的代码。【参考方案7】:您可以在cellForRowAtIndexPath
中为新添加的单元格应用验证。
在.h文件中声明一个变量。
Outgoing *lastOutgoing;
作为viewDidLoad
方法中的初始lastOutgoing = nil
。一旦添加了新对象。在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];
希望对你有所帮助..
【讨论】:
最初,不添加新单元格,它会改变最后一个单元格的颜色。 @viral - 您不希望第一次加载表格时最后一个单元格颜色不同,仅在添加新单元格/行时更改颜色? @the_UB,刚刚对初始数据加载应用了另一项验证。 @None 我无法找到输入该方法的数组。你的是dataArray,但我不知道我的是什么 @John,你不需要那个代码。刚刚删除。以上是关于更改单元格的背景颜色的主要内容,如果未能解决你的问题,请参考以下文章
更改每个其他单元格的 UItableViewCell 背景颜色
iOS UITableView:更改表格的背景颜色会覆盖单元格的背景颜色
选中时更改 UICollectionView 单元格的背景和标签颜色