问题
I'm building an open source version of the native iPhone Messages app called AcaniChat. Each message is represented by a UITableViewCell
with the timestamp at the top of the cell and the message below it.
Now, I'm implementing conditional timestamps, i.e., only show the timestamp for a message if that message is the first cell or if it's been 15 minutes since the last timestamp shown.
When I go into edit mode for a conversation in the iPhone Messages app, it looks like each timestamp is its own cell. I'd like to do the same because eventually, I'd like to add edit mode support too.
So then, based on the
indexPath
, how will I know whether to return a timestamp or a message cell?I'm using an
NSFetchedResultsController
for the messages. So, how do I keep track of the number of timestamps I've shown before a certain message so that I can use this as an offset when callingMessage *msg = [fetchedResultsController_ objectAtIndexPath:indexPath];
?
回答1:
In viewDidLoad
, after making the fetchRequest
for Message
objects, iterate through the results and create an NSMutableArray *cellMap
, which you should also make an ivar. Set each element of cellMap
to currentTimestamp
or message
, depending on the previousTimestamp
.
Check out the ChatViewController of AcaniChat for the solution.
来源:https://stackoverflow.com/questions/5561688/is-each-timestamp-in-the-iphone-messages-app-its-own-cell