JSQMessagesViewController:我的消息没有显示

时间:2017-04-09 16:34:40

标签: ios objective-c jsqmessagesviewcontroller

我的示例项目上有JSQMessageViewController,抱歉,我第一次使用它,意味着我基本上是新手,当我运行我的项目时,当我输入一条消息并点击发送时,我的消息不是出现

我的.h文件

    //
//  ViewController.h
//  JsqSimple
//
//  Created by Rawand Ahmed Shaswar on 4/9/17.
//  Copyright © 2017 ABA Group. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <JSQMessagesViewController/JSQMessages.h>

@interface ViewController : JSQMessagesViewController {

}

@end

还有,

我的.m文件

    //
//  ViewController.m
//  JsqSimple
//
//  Created by Rawand Ahmed Shaswar on 4/9/17.
//  Copyright © 2017 ABA Group. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.senderDisplayName = @"Fiko";
    self.senderId = @"Erick";

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSString *)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date {
    self.senderId = senderId;
    self.senderDisplayName = senderDisplayName;
    [JSQMessagesViewController messagesViewController];
    [self finishSendingMessage];
}

@end

非常感谢

1 个答案:

答案 0 :(得分:2)

To show the text something similar to ios message you can do something like this

//Array to keep the text's
NSMutableArray *message=nil;

-(void)didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSString *)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date{

    //creating the message object with message;
    JSQMessage *me=[[JSQMessage alloc]initWithSenderId:senderId senderDisplayName:senderDisplayName date:[NSDate distantPast] text:text];

    //initializing the array if its first time
    if(message==nil){
        message=[[NSMutableArray alloc]init];
    }
    //adding message to array 
    [message addObject:me];

    //Reloading the collectionView to get the update
    [self.collectionView reloadData];

}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    NSLog(@"number of message:%lu",(unsigned long)[message count]);
    // counting & returning total number of message
    return [message count];

}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];

    return cell;
}
-(id<JSQMessageData>)collectionView:(JSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPath:(NSIndexPath *)indexPath{

    return message[indexPath.item];
}

-(id<JSQMessageBubbleImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView messageBubbleImageDataForItemAtIndexPath:(NSIndexPath *)indexPath{

    JSQMessagesBubbleImageFactory *bubbleFactory=[[JSQMessagesBubbleImageFactory alloc]init];

//You can change the  bubble color here
    return [bubbleFactory outgoingMessagesBubbleImageWithColor:[UIColor greenColor]];
}

-(id<JSQMessageAvatarImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView avatarImageDataForItemAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}

Hope it helps...