简体   繁体   中英

Multi-selection behavior at user selection differs from setSelected:animated:

I have a UITableView that displays contacts from AddressBook. I have the multi-selection editing style enabled. When I present the tableView, it works as intended. The user taps a row and the red checkmark to the left side of the cell fills in. Great.

I save these selections in an array property on the presenting view controller. When I show my table again, I would like for these cells to remain selected (ie, not blue highlighted, just with the red check mark still filled in). In cellForRowAtIndexPath: I get the object for the cell at that index path, test whether the presenting view controller's array contains that object, and if so [cell setSelected:YES];

It seems to me that this should result in the same selected appearance as when the user touches a cell (just the red check mark). Instead, the red check does fill in but also the cell highlights blue momentarily and the text turns white permanently, creating the impression that the contact's name has disappeared. I'm happy to post code if it's helpful but this seems like maybe just a quirk of UITableView that someone might be familiar with? EDIT: This image shows the kind of checkmarks that I have (and want). I can't piece together though why when I call [cell setSelected:YES]; it behaves differently than it does in response to a touch. What you're looking at here is after I call [cell setSelected:YES];. And it looks almost how I want but the names have disappeeared. And I don't want the cell to highlight when the list appears.

在此处输入图片说明

If I understand the visual effect you're looking for, you should be getting it by using cell.accessoryType = UITableViewCellAccessoryCheckmark; rather than [cell setSelected:YES]; .

This is resolved. In think I was making problems for myself by trying to setSelected in cellForRowAtIndexPath. Maybe the table view's reusing of the cells was making it act crazy?

The solution for getting the cells to select correctly between presentations of the multi-selection style table was to store the selected indexes at viewWillDisappear. I gave the presenting view controller a property NSArray *selectedIndexes , and in viewWillDisappear called

[self.presentingViewController setSelectedIndexes:[myTableView indexPathsForSelectedRows]];

Then in viewWillAppear, I put that array into an ivar, iterated through it and called [myTableView selectRowForIndexPath:[selectedIndexes objectAtIndex:i] animated:YES]; That did the trick. Thanks to user523234 for the comment. Helped get me thinking on the right track.

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