gpt4 book ai didi

objective-c - 如何循环遍历数组并将 UITableView 中的文本变灰

转载 作者:行者123 更新时间:2023-11-29 13:26:22 25 4
gpt4 key购买 nike

我有一个在 viewWillAppear 中创建的 array ,控制台中的结果如下所示

data SecArray........... (
(
"<NSIndexPath 0x7f17630> 2 indexes [0, 0]"
),
(
"<NSIndexPath 0x7f96430> 2 indexes [1, 0]",
"<NSIndexPath 0x7f99c70> 2 indexes [1, 1]",
"<NSIndexPath 0x7f091a0> 2 indexes [1, 2]"
),
(
"<NSIndexPath 0xe43e8d0> 2 indexes [2, 0]"
)
)

我正在尝试使用以下代码使出现在 cellForRowAtIndexPath 中的 secArray 中的项目的文本变灰

        if ([self.secArray count] > 0) {

NSLog( @"GrayText Statement...1...");

if ([self.secArray containsObject:indexPath]) {

cell.textLabel.textColor = [UIColor grayColor];
NSLog( @"GrayText Statement...2...");

}


}

不明白为什么第二个 if-statement 没有被击中……我错过了什么?获取 secArray 中的每个 indexPath 并将其变灰的正确方法是什么?谢谢。

最佳答案

猜测您的意图是存储“无效”索引数组。不想问为什么,但可能有更简单的方法来实现您的意图。

几乎可以肯定,您存储在 secArray 中的 NSIndexPath 对象的实际实例与 cellForRowAtIndexPath 传递给您的实例不同。 (cellForRowAtIndexPath 将创建 NSIndexPath 的新实例;您正在存储“旧”实例。)

您需要比较它们是否相等,而不是检查数组是否包含特定实例:

for (NSIndexPath *path in self.secArray) {
if ([path compare:indexPath] == NSOrderedSame) {
cell.textLabel.textColor = [UIColor grayColor];
}
}

关于objective-c - 如何循环遍历数组并将 UITableView 中的文本变灰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com