简体   繁体   English

更改UITableView中一些选定的表格视图单元格文本的颜色

[英]Changing the color of a few selected table view cells text in UITableView

I have a table view with 8 cells. 我有一个包含8个单元格的表格视图。 Each contain a label. 每个都包含一个标签。 For the last 4 cells I want the label's text property to have a blue color. 对于最后4个单元格,我希望标签的text属性具有蓝色。 For the first four cells I want them to have a black color. 对于前四个单元格,我希望它们具有黑色。

I set the text color to blue in cellForRowAt indexPath with the following code: 我使用以下代码在cellForRowAt indexPath中将文本颜色设置为蓝色:

if indexPath.row > 4 {
    cell.label.textColor = UIColor.blue
}

This changes the color to blue, but when the user scrolls down and then up again the first two cell labels also have a blue color, because the cells got reused. 这会将颜色更改为蓝色,但是当用户向下滚动然后再次向上滚动时,前两个单元格标签也具有蓝色,因为单元已被重用。

How can I solve this? 我该如何解决? Thanks. 谢谢。

You will have to add else part into your cellForRow method since the dequeuing problem or Cell Reuse Issue. 由于出队问题或单元重用问题,您将不得不在cellForRow方法中添加其他部分。

cell.label.textColor = indexPath.row > 4 ? .blue : .black

Note: Always remember to add else part into cellForRow Method, Otherwise it behaves weirdly. 注意:始终记得将else部分添加到cellForRow方法中,否则它的行为很奇怪。

First, your code in its current state sets the color of all the cells whose indexes are greater than 4 to blue , not only the last 4 cells. 首先,处于当前状态的代码会将索引大于4的所有单元格的颜色设置为blue ,而不仅仅是最后4个单元格。 You need to change your condition. 您需要更改条件。

And after that, you need to add an else statement to handle other cells color, the cells who do not fulfill the condition. 然后,您需要添加else语句来处理其他不满足条件的单元格颜色。 This is because the cells are reused, so they always need to have their color set. 这是因为单元被重用,所以它们总是需要设置颜色。

So your code should look something like this: 因此,您的代码应如下所示:

if indexPath.row > numberOfRows - 4 {
    cell.label.textColor = .blue
} else {
    cell.label.textColor = .black
}

Or as mentioned by @jarvis, you can use ternary operator to make your code more compact, like this: 或如@jarvis所述,您可以使用三元运算符使您的代码更紧凑,如下所示:

cell.label.textColor = indexPath.row > numberOfRows - 4 ? .blue : .black

numberOfRows is the number of rows the table view has. numberOfRows是表视图具有的行数。

You need to set else condition too so that it won't change 您还需要设置其他条件,以便它不会改变

if indexPath.row > 4 {
    cell.label.textColor = UIColor.blue
}
else {
    cell.label.textColor = UIColor.black
}

That's because because indeed the cell is being reused. 这是因为确实确实在重复使用该单元。 To solve that, you need a logic what to do with the cells/rows not satisfying your condition indexPath.row > 4 . 为了解决这个问题,您需要一种逻辑来处理不满足条件的单元格/行indexPath.row > 4 :) :)

That's because because indeed the cell is being reused. 这是因为确实确实在重复使用该单元。

Make sure to use else condition everytime in UITableView and UICollectionView so value will not interchange during scroalling. 确保每次在UITableViewUICollectionView else使用else条件,这样在展开时值不会互换。

Here is 2 method you can follow so that color will not interchange. 您可以按照以下两种方法进行操作,以使颜色不会互换。

cell.label.textColor = indexPath.row > 4 ? .blue : .black

OR 要么

if indexPath.row > 4 {
    cell.label.textColor = UIColor.blue
}
else {
    cell.label.textColor = UIColor.black
}

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

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