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

时间:2016-07-01 04:53:58

标签: ios objective-c iphone uitableview

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

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

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

  • 如果arr2值不为null,则显示聊天消息
  • 如果arr3值不为空,则在聊天气泡中显示图片
  • 如果arr2值为null,则我想隐藏lbldesc并仅显示imv图库
  • 如果arr3值为null,则隐藏imv图库图片并启用lblDesc

但我有这样的例外:

  

由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' *** NSAllocateMemoryPages(4294967295)失败'`

这是我的代码:

-(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;

}

2 个答案:

答案 0 :(得分:2)

我使用了here中的PTSMessagingCell

我已将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];
}

希望这有帮助。

答案 1 :(得分:0)

创建自定义单元格并在后台线程中加载图像将提供更好的UI性能