mirror of
https://github.com/zhigang1992/MessagesTableViewController.git
synced 2026-06-18 07:09:20 +08:00
refactored to use delegates, more iOS-like. now must implement JSMessagesViewDelegate and JSMessagesViewDatasource.
This commit is contained in:
@@ -49,22 +49,37 @@ typedef enum {
|
||||
|
||||
|
||||
|
||||
@protocol JSMessagesViewDelegate <NSObject>
|
||||
@required
|
||||
- (void)sendPressed:(UIButton *)sender withText:(NSString *)text;
|
||||
- (JSBubbleMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
|
||||
- (JSMessagesViewTimestampPolicy)timestampPolicyForMessagesView;
|
||||
- (BOOL)hasTimestampForRowAtIndexPath:(NSIndexPath *)indexPath;
|
||||
@end
|
||||
|
||||
|
||||
|
||||
@protocol JSMessagesViewDataSource <NSObject>
|
||||
@required
|
||||
- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath;
|
||||
- (NSDate *)timestampForRowAtIndexPath:(NSIndexPath *)indexPath;
|
||||
@end
|
||||
|
||||
|
||||
|
||||
@interface JSMessagesViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextViewDelegate>
|
||||
|
||||
@property (assign, nonatomic) JSMessagesViewTimestampPolicy timestampPolicy;
|
||||
@property (weak, nonatomic) id<JSMessagesViewDelegate> delegate;
|
||||
@property (weak, nonatomic) id<JSMessagesViewDataSource> dataSource;
|
||||
@property (strong, nonatomic) UITableView *tableView;
|
||||
@property (strong, nonatomic) JSMessageInputView *inputView;
|
||||
@property (assign, nonatomic) CGFloat previousTextViewContentHeight;
|
||||
|
||||
#pragma mark - Actions
|
||||
- (void)sendPressed:(UIButton *)sender withText:(NSString *)text;
|
||||
- (void)sendPressed:(UIButton *)sender;
|
||||
- (void)handleSwipe:(UIGestureRecognizer *)guestureRecognizer;
|
||||
|
||||
#pragma mark - Messages view controller
|
||||
- (JSBubbleMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
|
||||
- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath;
|
||||
- (NSDate *)timestampForRowAtIndexPath:(NSIndexPath *)indexPath;
|
||||
- (BOOL)shouldHaveTimestampForRowAtIndexPath:(NSIndexPath *)indexPath;
|
||||
- (void)finishSend;
|
||||
- (void)setBackgroundColor:(UIColor *)color;
|
||||
|
||||
@@ -81,7 +81,6 @@
|
||||
{
|
||||
[super viewDidLoad];
|
||||
[self setup];
|
||||
self.timestampPolicy = JSMessagesViewTimestampPolicyEveryThree;
|
||||
}
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated
|
||||
@@ -130,12 +129,10 @@
|
||||
}
|
||||
|
||||
#pragma mark - Actions
|
||||
- (void)sendPressed:(UIButton *)sender withText:(NSString *)text { } // override in subclass
|
||||
|
||||
- (void)sendPressed:(UIButton *)sender
|
||||
{
|
||||
[self sendPressed:sender
|
||||
withText:[self.inputView.textView.text trimWhitespace]];
|
||||
[self.delegate sendPressed:sender
|
||||
withText:[self.inputView.textView.text trimWhitespace]];
|
||||
}
|
||||
|
||||
- (void)handleSwipe:(UIGestureRecognizer *)guestureRecognizer
|
||||
@@ -156,7 +153,7 @@
|
||||
|
||||
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
JSBubbleMessageStyle style = [self messageStyleForRowAtIndexPath:indexPath];
|
||||
JSBubbleMessageStyle style = [self.delegate messageStyleForRowAtIndexPath:indexPath];
|
||||
BOOL hasTimestamp = [self shouldHaveTimestampForRowAtIndexPath:indexPath];
|
||||
|
||||
NSString *CellID = [NSString stringWithFormat:@"MessageCell_%d_%d", style, hasTimestamp];
|
||||
@@ -169,9 +166,9 @@
|
||||
}
|
||||
|
||||
if(hasTimestamp)
|
||||
[cell setTimestamp:[self timestampForRowAtIndexPath:indexPath]];
|
||||
[cell setTimestamp:[self.dataSource timestampForRowAtIndexPath:indexPath]];
|
||||
|
||||
[cell setMessage:[self textForRowAtIndexPath:indexPath]];
|
||||
[cell setMessage:[self.dataSource textForRowAtIndexPath:indexPath]];
|
||||
[cell setBackgroundColor:tableView.backgroundColor];
|
||||
|
||||
return cell;
|
||||
@@ -182,28 +179,13 @@
|
||||
{
|
||||
CGFloat dateHeight = [self shouldHaveTimestampForRowAtIndexPath:indexPath] ? DATE_LABEL_HEIGHT : 0.0f;
|
||||
|
||||
return [JSBubbleView cellHeightForText:[self textForRowAtIndexPath:indexPath]] + dateHeight;
|
||||
return [JSBubbleView cellHeightForText:[self.dataSource textForRowAtIndexPath:indexPath]] + dateHeight;
|
||||
}
|
||||
|
||||
#pragma mark - Messages view controller
|
||||
- (JSBubbleMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath
|
||||
- (BOOL)shouldHaveTimestampForRowAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return 0; // Override in subclass
|
||||
}
|
||||
|
||||
- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return nil; // Override in subclass
|
||||
}
|
||||
|
||||
- (NSDate *)timestampForRowAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
return nil; // Override in subclass
|
||||
}
|
||||
|
||||
- (BOOL)shouldHaveTimestampForRowAtIndexPath:(NSIndexPath *)indexPath // Override in subclass if using JSMessagesViewTimestampPolicyCustom
|
||||
{
|
||||
switch (self.timestampPolicy) {
|
||||
switch ([self.delegate timestampPolicyForMessagesView]) {
|
||||
case JSMessagesViewTimestampPolicyAll:
|
||||
return YES;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user