开发者

iOS design question: How should switching between days be implemented in a table view?

I've got a bad case of analysis paralysis on this one so I thought I'd open it up to SO:

I have a table view, the contents of which represent activities that took place on a given day. When the app loads, today's data is downloaded, parsed (both by a central singleton), and displayed. 开发者_StackOverflow社区 Simple enough, and it works great.

I'd like to add the ability to go forward/back between days, using some kind of control at the top. An excellent example would be the day view of the iOS Calendar app.

Or, alternatively, I could init and push new views using a navigation bar and navigationItem.rightBarButtonItem & navigationItem.leftBarButtonItem.

What's the best approach here, and how should it be implemented (e.g., use another intermediary view or handle everything from the table view that actually displays the data)?


I've implemented something almost identical to this in an application I am developing for a client at the moment.

Firstly I would expose a method on your table view controller where you can set the day to display data for:

- (void)setDayToDisplayDataFor:(NSDate *)date animated:(BOOL)animated;

I've chosen NSDate there and you could for example show data for the next 24 hours starting from that date. Or you can choose another method, whichever works best for you and your data model. An alternative could be an integer representing the number of days backwards to look.

I would then develop a custom control (a subclass of UIView) with two left/right arrow buttons on either end and the date in the middle. You can either stick this in the table view's header so it scrolls off the screen or have it above the table view entirely so it remains there when scrolling. Something like this:

iOS design question: How should switching between days be implemented in a table view?

When the user taps a button to go forwards/backwards you can slide the current date label out to the left or right and slide the new date in. That gives a visual cue to what has happened.

The control should also call out to a delegate to inform it of the change in date. In this case the delegate is just your table view controller. So when the user taps the button you grab the new date from the custom date control and call your setDayToDisplayDataFor:animated: method to display the updated data. You could also animate this table view change if you like.


I think Mike's answer is a good one. I'd go one further to say that your view controller view might host two separate UITableViews (they can both use your view controller as a delegate...). The reason for this would be to enhance your ability to transition between the two separate views on a day-change.

Your view hierarchy might be:

YourCustomViewController <UITableViewDelegate, UTableViewDataSource, CustomDatePickerDelegate>
    view
      date-picker-control-view
      tableview1
      tableview2

With this arrangement you can use built-in view transition animations to switch between tableview1 and tableview2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜