gpt4 book ai didi

objective-c - UITableViewCell 中的 UISegmentedControl 在通过 dequeueReusableCellWithIdentifier 重用时更改段索引顺序

转载 作者:行者123 更新时间:2023-11-29 04:21:17 24 4
gpt4 key购买 nike

我有一个带有自定义 UITableCell 的 UITableView。这些 UITableCell 内部是 UISegmentedControl。我正在尝试更改第一个 UISegment 的色调颜色。这可以正常工作,直到通过 dequeueReusableCellWithIdentifier 重用 UITableCell。当向下滚动时重用的 UITableCell 开始出现时,最后一段而不是第一个段会变成蓝色。以下是cellforRowAtIndexPath中的相关代码:

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

CellIdentifier = @"CustomCell";
CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[CustomTableCell alloc] init];
}

[cell.segmentedControl setTintColor:GRAY_COLOR];
[[[cell.segmentedControl subviews] objectAtIndex:0] setTintColor:BLUE_COLOR];

...

return (UITableViewCell *) cell;
}

如果重要的话,此 UISegmentedControl 的 UISegmentedControlStyle 是 UISegmentedControlStyleBar。

最佳答案

您正在深入研究分段控件的未记录的 subview 结构。没有公共(public) API 可以为某一片段着色。当将来实现发生变化并且您获得的 subview 不响应“setTintColor:”方法时会发生什么?

您还依赖于 subview 的顺序。顺序可以改变。分段控件的实现可以随着时间的推移将 subview 移动到后面或前面。您不能假设 subview 0 是第一段的 View 。

此处最好的方法是子类化 UISegmentedControl 或创建自己的自定义控件,以正确地使分段具有特定颜色。

关于objective-c - UITableViewCell 中的 UISegmentedControl 在通过 dequeueReusableCellWithIdentifier 重用时更改段索引顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12961352/

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