滚动 UITableView 时,iOS 展开/选定的自定义 UITableViewCells 状态会发生变化 [英] iOS Expanded/Selected Custom UITableViewCells state changes when scrolling UITableView

查看:39
本文介绍了滚动 UITableView 时,iOS 展开/选定的自定义 UITableViewCells 状态会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 tableview 中有一个自定义单元格列表,当我滚动时,一切看起来都很好,并且单元格的顺序似乎相同.我的单元格有一些功能 - 当我选择一个单元格(并动态扩展)时,背景颜色会发生变化和其他一些自定义单元格属性.一旦我这样做然后我开始滚动,我什至没有接触过的不同单元格出现,选择(扩展)并且单元格只有在我手动将其选择为正确的数据时才会更新.我似乎看到了重复和各种疯狂.

I have a list of custom cells in my tableview and as I scroll everything appears to fine and the cells appear to be in the same order. I have some functionality with my cells - as i select a cell (and it dynamically expands)the background color changes and a few other custom cell properties. Once I do this and then I start scrolling, different cells that i haven't even touched before show up, selected(expanded) and the cell only updates when I select it manually to the correct data. I seem to see duplicates and all kinds of craziness.

我知道到目前为止这里有很多关于这个的帖子,但对我来说,到目前为止没有任何效果.想知道我能做些什么来阻止这种荒谬的行为.

I know there are LOTS of posts about this on here so far but for me, so far nothing has worked. Would like some input on what I could do to stop this ridiculous behavior.

我发布了一些代码,让您更好地了解我在做什么.我知道 'dequeueReusableCellWithIdentifier' 是罪魁祸首,但不知道替代方案.

I have posted some code to give you a better idea of what I am doing. I know that 'dequeueReusableCellWithIdentifier' is the culprit but don't know of an alternative.

作为旁注,这是一个tableview(它自己的xib),它是一个大视图(也是一个xib)的子视图.我也已经为 tableview 注册了笔尖.

As side notes, this is a tableview(its own xib) that is a child view of a large view (also a xib). I have also already registered the nib for the tableview.

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

    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:myIndentifier forIndexPath:indexPath];

    if(self.currentSelectedIndex){

        if(self.previousSelectedIndex){
                //collapse cell
                //configure cell in method(change background color etc)
         }
        else{
         //expand cell
         //configure cell in method(change background color etc)
         }
     }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    self.currentSelectedIndex = indexPath;

   [tableView beginUpdates];
    if(self.currentSelectedIndex){
        if(self.previousSelectedIndex && (self.previousSelectedIndex != self.currentSelectedIndex)){
        [tableView reloadRowsAtIndexPaths:@[self.currentSelectedIndex, self.previousSelectedIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        else{
            [tableView reloadRowsAtIndexPaths:@[self.currentSelectedIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
    [tableView endUpdates];

    if(self.previousSelectedIndex == self.currentSelectedIndex){
        self.previousSelectedIndex = nil;
    }
    else{
        self.previousSelectedIndex = self.currentSelectedIndex;
    }
}

我该怎么做,或者如何确保似乎"列表中没有其他任何内容被选中(展开)或防止在我滚动时出现重复项?我已经跟踪了我当前和上次选择的索引(如代码所示),所以我想我可以以某种方式使用它?

What can I do or how would i make sure that nothing else in the list 'seems' to be selected(expanded) or prevent from appearing to see duplicates as i scroll? I already keep track of my current and last selected index(as shown in the code) so I suppose that I could use that somehow?

推荐答案

重用出列单元

知道单元格被重用,所以 UITableViewCell 的外观在该单元格的整个生命周期中都是持久的.

Know that cells are re-used, so that the appearance of a UITableViewCell is persistent for the entire life of that cell.

这意味着,如果您没有显式reset单元格的所有呈现视图,而只是在cellForRowAtIndexPath 中原样返回它,那么您返回的可能是当前选中(或取消选中)缓存单元格.

This means that if you do not explicitly reset all the presentation view of your cell, and just returning it unchanged in cellForRowAtIndexPath, what you are returning may be a currently selected (or deselected) cached cell.

重置表格单元格的可能位置是prepareForReuse.

A possible location to reset a table cell is prepareForReuse.

设计说明:
您如何维护 self.currentSelectedIndexself.previousSelectedIndex?这通常非常危险,因为您正在尝试复制 UITableView 行为.例如,它不太可能与多重选择一起工作.设置 active 选择不太可能处理操作系统 didDeselectRowAtIndexPath 时的情况,例如键盘关闭.

Design note:
How are you maintaining self.currentSelectedIndex and self.previousSelectedIndex? This is typically quite dangerous, since you are attempting to replicate the UITableView behavior. It is for example, unlikely to work with multiple selection. Setting an active selection is unlikely handle situations when the OS didDeselectRowAtIndexPath, as a result of a keyboard dismissal for example.

这篇关于滚动 UITableView 时,iOS 展开/选定的自定义 UITableViewCells 状态会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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