简体   繁体   中英

iOS Expanded/Selected Custom UITableViewCells state changes when scrolling UITableView

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.

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.

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?

Dequeued Cells are Reused

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

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.

A possible location to reset a table cell is prepareForReuse .


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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