How does one design the datasource for a UITableView in order to sort with a UISegmentedControl?

大憨熊 提交于 2019-12-23 02:25:09

问题


I am in the process of designing and coding an iPhone application to display movie times. I currently have a Movie Class which contains:

  • NSString title
  • NSSDate releaseDate
  • NSSDate score

My UI is a simple UINavigationController which has a segmented control as it's title and UITableView to display the Movies setup in Interface Builder. The segmented control has 3 segments: Title, Opening Date, and Score (on RottenTomatoes). The data for the table view is currently provided by a NSMutableArray movies which is a property of the AppDelegate.

I know that I can register target actions using the following code, but I am unsure of how to sort the UITableView data at this point:

[segmentedControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];

How and where do I resort the UITableView whenever the UISegmentedControl selected segment changes?


回答1:


Also, you can sort the data with any of the sortArrayUsingxxx methods in NSArray.




回答2:


You can put some logic in the data source's tableView:cellForRowAtPath: method to check the current value of the UISegmentedControl and change the cell returned for each row as appropriate. Then, whenever the value of the segmented control changes, all you have to do is call

[tableView reloadData];

and you're all set.




回答3:


I have a segment control that can order/filter items in three different ways - because the number of items is different depending on the filter / order I use 3 different NSArray objects to hold the sorted lists.

So when I load up the data I put a version of it in the correct place in each array (or not in the array depending on the filter).

I have a call to get the array that uses the state of the segmented control to decide which array to return.

The method that calls an array is used in place of a normal array and this means all I have to do to change things when a segmented control is changed is call [tableView reloadData] and all is done for me.



来源:https://stackoverflow.com/questions/1050381/how-does-one-design-the-datasource-for-a-uitableview-in-order-to-sort-with-a-uis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!