0

what is the different between isSelected method of tableviewcell and selectRow(at:animated:scrollPosition:) of tableview when i want to set a cell to be selected?

i.e. cell.isSelected = true vs tableview.selectRow(at:index, animated: false, scrollPosition: .none)

I found most people use the latter,but i do not known why

Joiner
  • 81
  • 9
  • Which should you use? Depends entirely on your goal... Single select table view? Multi-select? Perform an Action on selection? No way to tell you without a complete explanation of what you want to do. What's the difference? Read the docs... write some code... see which works for your task. – DonMag Aug 03 '20 at 20:04
  • https://stackoverflow.com/questions/19295297/uitableviewcell-set-selected-initially, in this post, it has said : ```selectRow(at:animated:scrollPosition:) ```allow the user to deselect the cell. If this is missing it cannot be unselected – Joiner Aug 04 '20 at 02:17
  • because of the cell reuse so cannot be unselected? – Joiner Aug 04 '20 at 10:30
  • You still haven't told us ***why*** you want to manually change the selection.... it's *very* difficult to answer your question without context or a detailed description of what you're trying to accomplish. Either edit your question with details, or, better, experiment with the different methods yourself to get a full understanding. – DonMag Aug 04 '20 at 13:06

2 Answers2

0

one Work on the cell : means that you have pointer to it (mainly it is done in didSelectRow to unselect the row after pushing a detail view for example). The other tell the table view to select the cell not having to have a reference to it.

Ptit Xav
  • 3,006
  • 2
  • 6
  • 15
  • So which method should I use when I want to set a cell to be selected? – Joiner Aug 03 '20 at 17:37
  • When creatong the cell cell.isSelected. If other place in view controller managing the tableview selectRow – Ptit Xav Aug 03 '20 at 18:57
  • see this post: https://stackoverflow.com/questions/19295297/uitableviewcell-set-selected-initially post, it has said : ```selectRow(at:animated:scrollPosition:)``` allow the user to deselect the cell. If this is missing it cannot be unselected – Joiner Aug 04 '20 at 02:24
  • because of the cell reuse so cannot be unselected? – Joiner Aug 04 '20 at 10:30
0

selectRow(at:animated:scrollPosition:)

Selects a row in the table view identified by index path, optionally scrolling the row to a location in the table view.

cell.isSelected

isSelected bool affects the appearance of the cell. the default value is false.

For more info:

https://developer.apple.com/documentation/uikit/uitableviewcell/1623263-isselected https://developer.apple.com/documentation/uikit/uitableview/1614875-selectrow

Prakash Shaiva
  • 1,047
  • 8
  • 12
  • So which method should I use when I want to set a cell to be selected? And why – Joiner Aug 03 '20 at 17:36
  • @Joiner It depends on your requirement, if you have the cell object in the place where you want to set a cell to be selected, go with isSelected else go with selectRow(at:animated:scrollPosition:) – Prakash Shaiva Aug 03 '20 at 17:43
  • https://stackoverflow.com/questions/19295297/uitableviewcell-set-selected-initially, in this post, it has said : ```selectRow(at:animated:scrollPosition:) ``` allow the user to deselect the cell. If this is missing it cannot be unselected, so it seems recommended use ```selectRow(at:animated:scrollPosition:) ``` – Joiner Aug 04 '20 at 02:20
  • because of the cell reuse so cannot be unselected? – Joiner Aug 04 '20 at 10:30