gpt4 book ai didi

ios - 自定义 UITableViewCell 上的 UIGestureRecognizer。轻敲多个细胞

转载 作者:行者123 更新时间:2023-11-29 10:27:20 24 4
gpt4 key购买 nike

我在这个奇怪的问题上到处搜索都无济于事。我有一个自定义的子类 UITableViewCell。正面和背面的两个 View ,点击时单元格翻转。这是这样做的:

    - (IBAction)cellFrontTapped:(id)sender {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
if (_cellFlag == 0)
{
_cellFlag = 1;
[UIView transitionFromView:_cellFrontView toView:_cellBackView
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromTop | UIViewAnimationOptionShowHideTransitionViews
completion:NULL];
[self performSelector:@selector(cellBackTapped:) withObject:self afterDelay:15.0];
}
}
- (IBAction)cellBackTapped:(id)sender {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
if (_cellFlag == 1)
{
_cellFlag = 0;
[UIView transitionFromView:_cellBackView toView:_cellFrontView
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionShowHideTransitionViews
completion:NULL];
}
}

这只适用于几个单元格。最多几页值(value)。但是当我加载一个更大的数据集时,点击一个单元格会像预期的那样翻转它,但也会翻转其他单元格。我意识到这是因为单元格被 dequeueReusableCellWithIdentifier 重新使用,但我无法破译如何防止它。

我已经尝试将它实现为一个 UIGestureRecognizer。我试过像这样使用 didSelectRowAtIndexPath:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = (MyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

if (cell.cellFlag == 0)
{
[cell cellFrontTapped];
}
else
{
[cell cellBackTapped];
}
}

这些都可以正确翻转单元格,但点击事件总是翻转列表中的其他单元格。我发现的唯一解决方案是不要像这样使用 dequeueReusableCellWithIdentifier:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

[self configureCell:cell atIndexPath:indexPath];

return cell;
}

这是我当前的子类实现:

@implementation MyTableViewCell

- (void)awakeFromNib {
// Adding the recognizer here produced the same result
//UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapped)];
//tap.delegate = self;
//[self addGestureRecognizer:tap];
_cellFlag = 0;
}

- (void)cellFrontTapped {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
_cellFlag = 1;
[UIView transitionFromView:_cellFrontView toView:_cellBackView
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromTop | UIViewAnimationOptionShowHideTransitionViews
completion:NULL];
[self performSelector:@selector(cellBackTapped) withObject:self afterDelay:15.0];
}

- (void)cellBackTapped {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
_cellFlag = 0;
[UIView transitionFromView:_cellBackView toView:_cellFrontView
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionShowHideTransitionViews
completion:NULL];
}

-(void)cellTapped
{
if (_cellFlag == 0)
{
[self cellFrontTapped];
}
else
{
[self cellBackTapped];
}
}

@end

这是`configureCell:atIndexpath:

- (void)configureCell:(MyTableViewCell*)cell atIndexPath:(NSIndexPath*)indexPath
{
NSManagedObject *t = [[self fetchedResultsController] objectAtIndexPath:indexPath];
//here i set the labels and such
cell.cellBackAmountLabel.text = [t.amount formattedAmount];
//added
if ([self.flippedCellIndexes containsObject:indexPath])
{
[cell.contentView bringSubviewToFront:cell.cellBackView];
}
else
{
[cell.contentView bringSubviewToFront:cell.cellFrontView];
}
}

我很迷茫。在我的案例中,不使用 dequeueReusableCellWithIdentifier 会对性能产生重大影响。帮助!

最佳答案

我建议将模型与 View 分开。这意味着您应该跟踪哪些单元格应该呈现为翻转,而不是在单元格本身上。假设您在 View Controller 中声明了一个名为 flippedIndexesNSArray(甚至可能是 NSMutableArray)。

如果你的数据集像你说的那么大,那么你可能应该使用稀疏数组而不是 NSArray。可以使用 NSMutableDictionary 轻松实现稀疏数组。 NSMutableSet 也可以工作。

当单元格出列时,您将检查应该翻转哪些单元格,可能是在您的 configureCell:atIndexPath: 方法中。基本上,如果 indexPath 显示在您的稀疏数组上或设置为翻转的单元格。这意味着删除您在单元格上声明的 cellFlag 属性,并根据我提到的模型切换其状态。要翻转单元格,请检查给定 indexPathflippedIndexes 属性并采取相应措施。像这样:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = (MyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

if (![self.flippedIndexes containsObject:indexPath])
{
[cell cellFrontTapped];
[self.flippedIndexes addObject:indexPath];
}
else
{
[cell cellBackTapped];
[self.flippedIndexes removeObject:indexPath];
}
}

这里我假设为 flippedIndexes 属性使用 NSMutableSet。请注意,只有当您还在 configureCell:atIndexPath: 中检查模型时,这才会正常工作,否则您将在滚动时神奇地清除单元格。

这个故事的寓意是:不要在排队的单元格中存储状态信息。

关于ios - 自定义 UITableViewCell 上的 UIGestureRecognizer。轻敲多个细胞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31497662/

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