简体   繁体   English

在UITableViewCell的亮点上更改accessoryView

[英]Changing accessoryView on highlight of UITableViewCell

I'm trying to get my UITableViewCell to change it's accessoryView on highlight. 我正在尝试让我的UITableViewCell在高亮时改变它的accessoryView。 So the accessory is darker when highlighted. 所以配件在突出显示时会变暗。 I've managed to make it work pretty well so far with one exception. 到目前为止,我已经设法让它工作得非常好,只有一个例外。 When a cell is pressed it causes the cell to highlight but when that press is moved and the highlight is cancelled the accessoryView keeps its highlighted image. 当按下一个单元格时,它会使单元格突出显示,但当移动该按下并取消突出显示时,accessoryView会保留其突出显示的图像。

Is there any way to detect if the highlight was cancelled so I can change the accessoryView back to what it should be? 有没有办法检测突出显示是否被取消所以我可以将accessoryView更改回应该是什么? Would I need to create a subclass of UITableViewCell in order to accomplish this? 我是否需要创建UITableViewCell的子类才能实现此目的? Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks! 谢谢!

Normal State: 正常状态: 正常细胞状态

Highlighted State 突出的国家 在此输入图像描述

After pressing and moving the touch (what I'd like to avoid): 按下并移动触摸后(我想避免): 在此输入图像描述

 - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    UIImageView *accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessoryDark.png"]];
    cell.accessoryView = accessoryView;
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    UIImageView *accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory.png"]];
    cell.accessoryView = accessoryView;
}

I tried reproducing your setup and what I found is that the indexPath being passed to didUnhighlightRowAtIndexPath included an NSNotFound row. 我尝试重现你的设置,我发现传递给didUnhighlightRowAtIndexPath包含一个NSNotFound行。 So you might want to check that the cell you're getting in didUnhighlightRowAtIndexPath is non-nil. 因此,您可能想要检查您在didUnhighlightRowAtIndexPath获取的单元格是否为非零。 If it turns out that you're getting the wrong indexPaths passed to didUnhighlightRowAtIndexPath then I think we can chalk it up to a bug in the new API and you're going to want to use didSelectRow and didDeselectRow, or subclass UITableViewCell and watch for changes to the selected and highlighted states in your subclass. 如果你得到错误的indexPaths传递给didUnhighlightRowAtIndexPath,那么我认为我们可以将它归结为新API中的一个错误,你会想要使用didSelectRow和didDeselectRow,或者是UITableViewCell的子类并注意变化到子类中的选定和突出显示状态。

Another thing you could try is to set the accessory view to 您可以尝试的另一件事是将附件视图设置为

[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory"] highlightedImage:[UIImage imageNamed:@"accessoryDark"]];

Then (in theory) the accessory view will automatically show the correct highlighted or not highlighted image based on the highlighted state of the cell, and you won't have to watch for those updates in your delegate methods. 然后(理论上)附件视图将根据单元格的突出显示状态自动显示正确的突出显示或未突出显示的图像,您不必在委托方法中查看这些更新。

Try using didSelectRow... and didDeselectRow... . 尝试使用didSelectRow...didDeselectRow... You should also keep a ref to the index path of the selected cell. 您还应该保留ref到所选单元格的索引路径。 That way the scrolling would not mess up your cells (of course, in the cellForRow... you check whether the current cell is the selected one, and set the accessory view accordingly). 这样滚动就不会弄乱你的单元格(当然,在cellForRow...你检查当前单元格是否是选中的单元格,并相应地设置附件视图)。 Hope this helps! 希望这可以帮助!

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

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