UITableViewCell选择器setSelected:animated:被多次调用? [英] UITableViewCell selector setSelected:animated: gets called many times?

查看:779
本文介绍了UITableViewCell选择器setSelected:animated:被多次调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义UITableViewCell类中发现了 setSelected:animated:的奇怪行为。我发现如果单击表格中的单元格,会多次调用此函数。我想知道这是正常行为还是代码中的错误。

I've discovered a strange behavior with setSelected:animated: in my custom UITableViewCell class. I discovered that this function gets called multiple times if I click on a cell in my table. I am wondering if this is normal behavior or a bug in my code.

为了帮助调试,我修改了 setSelected:animated:在我的自定义UITableViewCell类实现中的函数:

To help with debugging, I've modified the setSelected:animated: function in my custom UITableViewCell class implementation as such:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];

// Configure the view for the selected state.
if (selected)
    NSLog(@"Yes %X", &self);
else
    NSLog(@"No %X", &self);

}

如果我点击模拟器中的单元格,这里是我在控制台得到的结果:

If I click on a cell in the simulator, here is what I get in the console:

2011-03-22 22:05:26.963 marketPulse[3294:207] Yes BFFFDDD0
2011-03-22 22:05:26.964 marketPulse[3294:207] Yes BFFFDE30

您会认为我只会输入1个条目,因为我只点击了1个单元格。

You would think that I would get only 1 entry, since I only clicked on 1 cell.

如果我之后单击其他单元格:

And if I click on a different cell after that:

2011-03-22 22:07:11.014 marketPulse[3294:207] No BFFFD890
2011-03-22 22:07:11.016 marketPulse[3294:207] No BFFFDD00
2011-03-22 22:07:11.017 marketPulse[3294:207] Yes BFFFDDD0
2011-03-22 22:07:11.017 marketPulse[3294:207] Yes BFFFDE30

如果我连续两次点击同一个单元格,我会得到2个以上

If I click on the same cell 2 times in a row, I get more than 2 Yes:

2011-03-22 22:08:41.067 marketPulse[3294:207] Yes BFFFDDD0
2011-03-22 22:08:41.068 marketPulse[3294:207] Yes BFFFDE30
2011-03-22 22:08:41.069 marketPulse[3294:207] Yes BFFFDE30

次数越多我点击同一个单元格,我会得到更多,如果我之后点击另一个单元格,我会得到很多

The more times I click the same cell, the more Yes I will get, and if I click on a different cell after that, I'll get a lot of No

我在NSLog之前放了一个断点,看看调试器,似乎所有重复的调用来自同一个对象。

I put a breakpoint before the NSLog, and looking at the debugger, it seems that all the repeated calls are coming from the same object.

以下是我的 tableView:cellForRowAtIndexPath:函数的一部分,因此您可以看到我的单元格是如何处理的:

Here is a part of my tableView:cellForRowAtIndexPath: function so you can see how my cells are being treated:

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


static NSString *ContentCellIdentifier = @"newsTableCellContent";

UITableViewCell *cell;


//index of cell data in tableData
NSUInteger index = indexPath.row / 2;

...

//content of story
else if( [indexPath row] % 2 == 1 ) {

    cell = [tableView dequeueReusableCellWithIdentifier:ContentCellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle]
                                    loadNibNamed:@"newsTableCells"
                                    owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ( [currentObject isKindOfClass:[newsTableCellContent class]] ) {
                cell = currentObject;
                break;
            }
        }
    }

    ((newsTableCellContent *)cell).content.text = [[tableData objectAtIndex:index] description];

}   

return cell;
}

一切正常,所以如果重复调用很难判断setSelected:animated:是有意还是无意。如果这是正常操作,我可以使用其他方法,但我想知道是否发生这种情况。

Everything works fine so its hard to tell if the repeat calls to setSelected:animated: are intentional or not. If this is normal operation, I can make do with another method, but I would just like to know if this is suppose to happen or not.

谢谢

推荐答案

发生的事情只是 UITableView 跟踪选择了哪些单元格桌子。

What's going on is simply that the UITableView keeps track of which cells are selected in the table.

由于在滚动大型表格视图时会重复使用单元格,因此表格视图必须将所选单元格的列表分开。不仅如此,但每当它重用一个单元格时,它必须设置其选定的属性,因为它可能使用了之前版本中旧的,无效的选择的状态。

Since cells are reused when you scroll through a large table view, the table view has to keep the list of selected cells separate. Not only that, but whenever it reuses a cell it has to set its selected property, because it may be using an old, invalid selected state from a previous incarnation.

当您点击一个单元格时,会发生以下几种情况:取消选择先前选择的单元格(使用 setSelected:)。新单元格突出显示。它被去突出显示(至少如果你点击,而不是按住手指),并且调用 setSelected:方法,因为选中了新单元格。那是一个。

When you tap a cell, several things happen: the previously selected cell is deselected (using setSelected:). The new cell is highlighted. It's de-highlighted (at least if you tap, instead of holding your finger down), and the setSelected: method is called because the new cell was selected. That's one.

第二个调用是一个延迟执行调用,可能是从表视图还不知道表的最终状态是什么的。此调用转到 _selectAllSelectedRows ,顾名思义,它在所有选定的行上调用'setSelected:animated:'。那是第二次通话。这样做的原因最有可能解决由于表视图处于转换而导致的潜在问题,但谁知道。

The second call is a delayed perform call, possibly from a point where the table view didn't yet know what the final state of the table would be. This call goes to _selectAllSelectedRows, which, as the name suggests, calls 'setSelected:animated:' on all selected rows. That's the second call. The reason for this is most likely to address potential issues due to the the table view being in a "transition", but who knows.

是否是错误是解释。重复调用的修复方法就是:

Whether it's a bug or not is up for interpretation. A fix for the duplicate calls is to simply do:

if (self.selected == selected) return;

在调用super之前(如果你不必调用super) self.selected == selected )。

right before the call to super (you do not have to call super if self.selected == selected).

这篇关于UITableViewCell选择器setSelected:animated:被多次调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆