简体   繁体   English

移动表格视图单元格后如何更改背景色?

[英]How to change the background color of a table view cell after it was moved?

I have a table view with 9 rows at minimum. 我有至少9行的表格视图。 The background colors of the cells will depend of the cell position. 单元格的背景颜色将取决于单元格的位置。 If it is bellow to 3 or below to 6 or below to 9, the background color changes (see the next image). 如果低于3或低于6或低于9,则背景颜色会更改(请参见下一张图像)。

替代文字

The problem is that this table view can be reordered and I have to maintain the same color pattern. 问题在于此表视图可以重新排序,我必须保持相同的颜色模式。 Example: if i change the 4th row to the 3th position, its background color must change from yellow to red. 例如:如果我将第4行更改为第3位,则其背景颜色必须从黄色更改为红色。

To get the different colors, I used the method - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath , but this method is called only when the cell will be displayed and isn't good for reordering issues (the cell is already displayed and the color don't change). 为了获得不同的颜色,我使用了- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath方法,但是仅当将显示该单元格并且不显示该单元格时才调用此方法对于重新排序问题(单元格已显示且颜色不变)不利。

I already tried to create a method to change the background color that is called just after the row was moved, but the color didn't changed (it seems that changing the background color with cell.backgroundColor property just works in - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath ). 我已经尝试创建一种方法来更改移动行后立即调用的背景颜色,但是颜色没有更改(似乎使用cell.backgroundColor属性更改背景颜色仅在- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath起作用- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath )。 :( :(

Any ideas how I can do that? 有什么想法可以做到吗?

Thanks in advance. 提前致谢。

Adjust the position in your data source however you are doing, then tell your data source to recalculate the colours for each item based on their current position. 调整数据源中的位置,但要这样做,然后告诉数据源根据每个项目的当前位置重新计算颜色。 Then reload your table view. 然后重新加载表格视图。 It should "just work". 它应该“正常工作”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM