gpt4 book ai didi

ios - 如何在 iOS 的 uitableviewcell 中显示消息和图像

转载 作者:行者123 更新时间:2023-12-01 20:08:38 28 4
gpt4 key购买 nike

我正在开发聊天应用程序。其实我有3个数组。第一个数组存储 用户名 第二个数组存储 聊天消息第三个数组存储 图片 .我正在从照片库中挑选图像。

当用户向聊天墙发送任何消息时,我会在气泡中显示消息。

所以我写了一些条件,如下所示:

  • 如果 arr2值不为空则显示聊天消息
  • 如果 arr3值不为 null 在聊天气泡中显示图像
  • 如果 arr2值为 null然后我想隐藏lbldesc并且只显示 imv画廊图片
  • 如果 arr3值为 null然后隐藏imv 图库图片并启用lblDesc

  • 但我有这样的异常(exception):

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** NSAllocateMemoryPages(4294967295) failed'`



    这是我的代码:
    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

    static NSString *CellIdentifier = @"cellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

    if (cell == nil) {

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    if (itemsDataArr.count>0) {

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(35, 3, 250, 80)];

    imgView.image = [UIImage imageNamed:@"speech-bubble-2-hi.png"];

    imgView.layer.borderColor = [UIColor clearColor].CGColor;

    imgView.tag = 5;

    [cell.contentView addSubview:imgView];

    NSArray *arr1=[[NSArray alloc]init];

    arr1=[itemsUserNameArr objectAtIndex:indexPath.row];

    NSString *strval=[arr1 objectAtIndex:0];

    lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(50, 5, 150, 20)];

    lblTitle.highlightedTextColor=[UIColor whiteColor];

    [cell.contentView addSubview:lblTitle];

    [lblTitle setFont:[UIFont boldSystemFontOfSize:14]];

    lblTitle.text=strval;

    [imgView addSubview:lblTitle];

    NSArray *arr2=[[NSArray alloc]init];

    arr2=[itemsDataArr objectAtIndex:indexPath.row];

    NSArray *arr3=[[NSArray alloc]init];

    arr3=[itemImgArray objectAtIndex:indexPath.row];

    if(![arr2 isEqual:@""])

    {

    NSString *strmsg=[arr2 objectAtIndex:0];

    lblDesc=[[UILabel alloc]initWithFrame:CGRectMake(50, 22, 300, 20)];

    lblDesc.highlightedTextColor=[UIColor whiteColor];

    lblDesc.font=[UIFont systemFontOfSize:12.0];

    [cell.contentView addSubview:lblDesc];

    [lblDesc setHidden:NO];

    lblDesc.text=strmsg;

    [imgView addSubview:lblDesc];

    // imv.hidden=YES;

    // [imv setHidden:true];

    }

    arr3=[itemImgArray objectAtIndex:indexPath.row];

    if(![arr3 isEqual:nil])

    {

    NSString *strImg=[arr3 objectAtIndex:0];

    NSData *data = [[NSData alloc] initWithData:[NSData dataFromBase64String:strImg]];

    //Now data is decoded. You can convert them to UIImage

    imv = [[UIImageView alloc]initWithFrame:CGRectMake(93,12, 50, 50)];

    imv.image=[UIImage imageWithData:data];

    [imgView addSubview:imv];

    [lblDesc setHidden:YES];

    // lblDesc.hidden=YES;

    }

    }

    return cell;

    }

    最佳答案

    我用过PTSMessagingCell来自 here :

    我用过messages文本/图像的数组。这是一个可能对您有所帮助的 ruf 样本。

    #import "ViewController.h"
    #import "PTSMessagingCell.h"

    @interface ViewController ()
    {
    NSMutableArray *messages;
    UITextView *sendMsg;
    UITableView *chatTable;
    }
    @end

    @implementation ViewController

    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    messages = [[NSMutableArray alloc] initWithObjects:@"Hello",[UIImage imageNamed:@"download.jpeg"],@"Check This Out", nil];

    chatTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-100)];
    chatTable.delegate = self;
    chatTable.dataSource = self;
    chatTable.separatorColor = [UIColor clearColor];
    [self.view addSubview:chatTable];
    chatTable.backgroundColor = [UIColor clearColor];


    sendMsg = [[UITextView alloc]initWithFrame:CGRectMake(0, chatTable.frame.size.height, self.view.frame.size.width-100, 100)];
    sendMsg.textColor = [UIColor blackColor];
    sendMsg.delegate = self;
    sendMsg.backgroundColor = [UIColor whiteColor];
    sendMsg.layer.cornerRadius = 3.0f;
    [self.view addSubview:sendMsg];

    UIButton *sendMsgbtn = [[UIButton alloc]initWithFrame:CGRectMake(sendMsg.frame.size.width, chatTable.frame.size.height, 100, 100)];
    [sendMsgbtn setTitle:@"SEND" forState:UIControlStateNormal];
    sendMsgbtn.backgroundColor = [UIColor redColor];
    [sendMsgbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sendMsgbtn addTarget:self action:@selector(sendClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sendMsgbtn];

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapReceived:)];
    [tapGestureRecognizer setDelegate:self];
    [chatTable addGestureRecognizer:tapGestureRecognizer];
    }

    -(void)sendClicked{
    [sendMsg resignFirstResponder];
    [chatTable reloadData];
    }

    #pragma mark - TableView Delegate and DataSource Methods

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [messages count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*This method sets up the table-view.*/

    static NSString* cellIdentifier = @"messagingCell";
    PTSMessagingCell * cell = (PTSMessagingCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
    cell = [[PTSMessagingCell alloc] initMessagingCellWithReuseIdentifier:cellIdentifier];
    }
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
    }

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if ([[messages objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]]) {
    return [(UIImage *)[messages objectAtIndex:indexPath.row] size].height;
    }
    else{
    const char *jsonString = [[NSString stringWithFormat:@"%@",[messages objectAtIndex:indexPath.row]] UTF8String];
    NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)];
    NSString *goodMsg = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
    CGSize messageSize = [PTSMessagingCell messageSize:goodMsg];
    NSLog(@"%f",messageSize.height + 2*[PTSMessagingCell textMarginVertical] + 40.0f);
    return messageSize.height + 2*[PTSMessagingCell textMarginVertical] + 40.0f;
    }

    }

    -(void)configureCell:(id)cell atIndexPath:(NSIndexPath *)indexPath {
    if (messages.count>0) {
    PTSMessagingCell* ccell = (PTSMessagingCell*)cell;

    if ([[messages objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]]) {

    ccell.sent = YES;
    ccell.avatarImageView.image = [UIImage imageNamed:[messages objectAtIndex:indexPath.row]];
    ccell.messageLabel.text = @"";
    }
    else{
    ccell.sent = YES;

    const char *jsonString = [[NSString stringWithFormat:@"%@",[messages objectAtIndex:indexPath.row]] UTF8String];
    NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)];
    NSString *goodMsg = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];

    ccell.messageLabel.text = [messages objectAtIndex:indexPath.row];
    }
    ccell.timeLabel.text = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterFullStyle];


    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapReceived:)];
    [tapGestureRecognizer setDelegate:self];
    [ccell addGestureRecognizer:tapGestureRecognizer];
    }
    }

    -(void)tapReceived:(UITapGestureRecognizer *)tapGestureRecognizer
    {
    // do something, like dismiss your view controller, picker, etc., etc.
    [sendMsg resignFirstResponder];
    }

    希望这可以帮助。

    关于ios - 如何在 iOS 的 uitableviewcell 中显示消息和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38137310/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com