Titanium JIRA Archive
Appcelerator Community (AC)

[AC-4657] ListView Retrieve Selected Item

GitHub Issuen/a
TypeImprovement
Priorityn/a
StatusResolved
ResolutionWon't Fix
Resolution Date2016-12-08T08:45:00.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsios, listview
ReporterNicholas Thurston
AssigneeShak Hossain
Created2016-12-07T21:11:16.000+0000
Updated2016-12-19T23:36:24.000+0000

Description

Would like a way to retrieve the selected list item at any given time. We can track the itemclick, but the "selected" status gets removed by various other actions (i.e. window blur, resetting the data, begin editing a row, etc.) thereby making whatever variable you've kept not match what is seen in the list. iOS has a method on UITableView for retrieving the selected index path: [indexPathForSelectedRow](https://developer.apple.com/reference/uikit/uitableview/1615000-indexpathforselectedrow?language=objc) This could also be solved by exposing the didDeselectRow delegate method: [tableView:didDeselectRowAtIndexPath:](https://developer.apple.com/reference/uikit/uitableviewdelegate/1614916-tableview?language=objc)

Comments

  1. Hans Knöchel 2016-12-08

    You would rather watch the itemclick event and set the index-pair or even itemId based on that. A new API for something that can be achieved that easy would rather produce possible loop-holes then supporting the general performance. Example:
       var selecedItem = null;
       
       list.addEventListener("itemclick", function(e) {
           selectedItem = e.itemId; // or {sectionIndex: e.sectionIndex, itemIndex: e.itemIndex}
       });
       
  2. Nicholas Thurston 2016-12-08

    Right - as I said, you could do that, but your variable would not match what is shown as selected after certain actions. E.g. The row background is normally red, my selected background color is blue, I cannot reliably say that my selectedItem variable will match the blue "selected" row with Titanium. I gave 3 examples that cause the item not to be "selected" anymore: 1. As soon as you swipe the item to reveal the edit actions, the item reverts to unselected. This is the killer because we don't even have an event here or a chance to unset our variable. 2. On window blur the item gets unselected, or more accurately when window focuses again. This one could be solved with event listeners on the window. 3. If you reset the data, the item gets unselected. This one is pretty obvious, but again forces more code to maintain your variable. The fact of the matter is that if your app depends on knowing which row is shown as selected at any given time, you cannot determine that with Titanium.
  3. Hans Knöchel 2016-12-18

    I understand your use-cases. You could try to add the following to your TiUIListViewProxy.m:
       -(id)selectedItem
       {
           NSIndexPath *selectedItem = [[self listView] pathForSearchPath:[[[self listView] tableView] indexPathForSelectedRow]];
           
           return @{
               @"itemIndex": NUMINTEGER(selectedItem.row ?: -1),
               @"sectionIndex": NUMINTEGER(selectedItem.section ?: -1),
               @"section": selectedItem.section ? [self sectionForIndex:selectedItem.section] : [NSNull null]
           };
       }
       
    and -(NSIndexPath*)pathForSearchPath:(NSIndexPath*)indexPath; in the TiUIListView.h. You can now call it with myTableView.selectedItem or myTableView.getSelectedItem(). Is that something you would come along with?
  4. Nicholas Thurston 2016-12-19

    Yes, this looks great. I will try it out soon. Thank you

JSON Source