2016-01-19 43 views
1

我正在使用UIScrollview並添加subView以顯示消息。滾動時給出received memory exception。我很難從15天內找到這個。請幫我解決這個問題。應用程序與接收到的內存崩潰使用圖像滾動滾動視圖時出現異常

要顯示我第一次加載5條消息。滾動後,根據數組計數添加子視圖。如果它是根據消息長度增加子視圖。如果它是長消息,則它與received memory exception崩潰。

這是我的代碼

-(void)createView:(float)yAxis 
{ 
    // NSLog(@"Create view %f",yAxis); 
    for (UIView * view in backgroundScroll.subviews) { 
     if (view.tag == 15) { 
      [view removeFromSuperview]; 
     } 
    } 

    for(int i=0;i<[pageMessageArray count];i++) 
    { 
     UIView *backgrounf_View=[self messageChildElements:yAxis :i]; 
     [del.msglistViewArray addObject:backgrounf_View]; 
     UIView *backgrounf_View1= [del.msglistViewArray objectAtIndex:i]; 

     [backgroundScroll addSubview:backgrounf_View1]; 

     yAxis=yAxis+oldY_Axis+65; 
    } 



    CGRect contentRect = CGRectZero; 
    for (UIView *view in backgroundScroll.subviews) 
    { 
     contentRect = CGRectUnion(contentRect, view.frame); 
    } 

    //UIView *backgrounf_View1= [del.msglistViewArray objectAtIndex:i]; 

    backgroundScroll.contentSize = CGSizeMake(backgroundScroll.frame.size.width, yAxis-10); 

    lblPermenentmessage.frame=CGRectMake(30, contentRect.size.height+20,800, 50); 
    //LastMessageYaxis=contentRect.size.height+20; 
    [email protected]"Messages are displayed for 24-48 hours and automatically deleted permanently."; 
} 

-(UIView *)messageChildElements:(float)yAxis :(int)index 
{ 
    // NSLog(@"Create view %f",yAxis); 

    communityObject *objMessage=[del.messageArray objectAtIndex:index]; 
    UIView *backgrounf_View=nil; 
    UITextView *txt_Message=nil; 
    UIButton *btnURL =nil; 
    UIButton *btnAttachment=nil; 
    UIButton *btnDate=nil; 
    UIButton *btnSendBy=nil; 

    UIButton *btnAttachmentName=nil; 
    UIButton *btnSendTo=nil; 


    NSString *labelText = objMessage.Message; 
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText]; 
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
    [paragraphStyle setLineSpacing:8]; 
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])]; 

    CGFloat height=[self getNewsize:objMessage.Message :backgroundScroll.frame.size.width-30]; 

    backgrounf_View=[[UIView alloc]initWithFrame:CGRectMake(26,yAxis,backgroundScroll.frame.size.width-30,height)]; 

    if([objMessage.inorout isEqualToString:@"out"]) 
    { 
     //ye 
     [backgrounf_View setBackgroundColor:[UIColor colorWithRed:251/255.0f green:247/255.0f blue:193/255.0f alpha:1.0f]]; 
     [[backgrounf_View layer] setBorderWidth:1.0f]; 
     [[backgrounf_View layer] setBorderColor:[UIColor colorWithRed:236/255.0f green:237/255.0f blue:178/255.0f alpha:1.0f].CGColor]; 
    } 
    else 
    { 
     //[cell.lblName addTarget:self action:@selector(replayClick:) forControlEvents:UIControlEventTouchUpInside]; 
     [backgrounf_View setBackgroundColor:[UIColor colorWithRed:251/255.0f green:215/255.0f blue:245/255.0f alpha:1.0f]]; 
     [[backgrounf_View layer] setBorderWidth:1.0f]; 
     [[backgrounf_View layer] setBorderColor:[UIColor colorWithRed:226/255.0f green:188/255.0f blue:218/255.0f alpha:1.0f].CGColor]; 
    } 


    // backgrounf_View.autoresizingMask=UIViewAutoresizingFlexibleWidth; 
    backgrounf_View.layer.cornerRadius=10; 
    //backgrounf_View.tag=15; 
    backgrounf_View.clipsToBounds=YES; 
    backgrounf_View.tag=objMessage.indexValue; 

    txt_Message = [[UITextView alloc] initWithFrame:CGRectMake(12, 6, backgroundScroll.frame.size.width-40, height)]; 
    txt_Message.backgroundColor=[UIColor redColor]; 
    [txt_Message setScrollEnabled:NO]; 
    [txt_Message setUserInteractionEnabled:NO]; 
    [txt_Message setBackgroundColor:[UIColor clearColor]]; 

    //txt_Message.autoresizingMask=UIViewAutoresizingFlexibleWidth; 

    txt_Message.attributedText=attributedString; 

    //[txt_Message sizeToFit]; 

    if([del.device isEqualToString:@"iphone"]) 
    { 
     [txt_Message setFont:[UIFont fontWithName:@"Arial" size:17]]; 
    } 
    else 
    { 
     [txt_Message setFont:[UIFont fontWithName:@"Arial" size:21]]; 
    } 

    [backgrounf_View addSubview:txt_Message]; 
    oldY_Axis=height+20; 

    if (objMessage.Link.length>0) 
    { 
     btnURL = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btnURL.frame = CGRectMake(11,height,backgroundScroll.frame.size.width-100,40); 
     [btnURL setTitle:objMessage.Link forState:UIControlStateNormal]; 
     btnURL.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
     btnURL.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; 
     // btnURL.backgroundColor=[UIColor redColor]; 
     btnURL.tag=objMessage.indexValue; 
     btnURL.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap; 
     btnURL.titleLabel.numberOfLines=2; 
     btnURL.titleLabel.font=[UIFont fontWithName:@"Arial" size:20]; 
     btnURL.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); 
     [btnURL setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     [backgrounf_View addSubview:btnURL]; 

     oldY_Axis=height+btnURL.frame.size.height+20; 
    } 

    if (objMessage.Filename.length>0) 
    { 
     btnAttachment= [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btnAttachment.frame = CGRectMake(11, oldY_Axis,40, 40); 
     btnAttachment.tag=objMessage.indexValue; 

     UIImage *buttonattachment=[UIImage imageNamed:@"attach.png"]; 
     [btnAttachment setBackgroundImage:buttonattachment forState:UIControlStateNormal]; 
     btnAttachment.userInteractionEnabled=YES; 

     btnAttachmentName = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btnAttachmentName.frame = CGRectMake(47,oldY_Axis+5,backgroundScroll.frame.size.width-150,60); 
     [btnAttachmentName setTitle:objMessage.Filename forState:UIControlStateNormal]; 
     btnAttachmentName.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
     btnAttachmentName.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; 
     btnAttachmentName.tag=objMessage.indexValue; 


     btnAttachmentName.titleLabel.font=[UIFont fontWithName:@"Arial" size:20]; 
     btnAttachmentName.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); 
     [btnAttachmentName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     [backgrounf_View addSubview:btnAttachment]; 
     [backgrounf_View addSubview:btnAttachmentName]; 

     oldY_Axis=oldY_Axis+btnAttachment.frame.size.height+30; 
    } 

    btnDate = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btnDate.frame = CGRectMake(11,oldY_Axis,130,30); 
    [btnDate setTitle:[NSString stringWithFormat:@"%@:",objMessage.Date] forState:UIControlStateNormal]; 
    btnDate.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    btnDate.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; 
    btnDate.tag=objMessage.indexValue; 
    btnDate.titleLabel.font=[UIFont fontWithName:@"Arial" size:15]; 
    btnDate.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); 
    [btnDate setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal]; 
    [backgrounf_View addSubview:btnDate]; 

    btnSendBy = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btnSendBy.frame = CGRectMake(127,oldY_Axis,backgroundScroll.frame.size.width-50,200); 
    [btnSendBy setTitle:objMessage.senderPerson forState:UIControlStateNormal]; 
    btnSendBy.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    btnSendBy.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; 
    btnSendBy.tag=objMessage.indexValue; 

    // btnURL.backgroundColor=[UIColor redColor]; 
    //btnSendBy.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap; 
    btnSendBy.titleLabel.font=[UIFont fontWithName:@"Arial" size:15]; 
    btnSendBy.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); 
    [btnSendBy setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal]; 
    [backgrounf_View addSubview:btnSendBy]; 

    btnSendTo = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btnSendTo.frame = CGRectMake(11,oldY_Axis+25,backgroundScroll.frame.size.width-50,200); 
    [btnSendTo setTitle:objMessage.senderPerson forState:UIControlStateNormal]; 
    btnSendTo.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    btnSendTo.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; 
    // btnURL.backgroundColor=[UIColor redColor]; 
    //btnSendBy.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap; 
    btnSendTo.titleLabel.font=[UIFont fontWithName:@"Arial" size:15]; 
    btnSendTo.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); 
    [btnSendTo setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal]; 
    [backgrounf_View addSubview:btnSendTo]; 


    NSString *[email protected]""; 

    if([del.communityId isEqualToString:@"none"]) 
    { 
     communityName=objMessage.Community;    
     communityName=[NSString stringWithFormat:@"%@: ",objMessage.Community]; 
    } 

    //NSLog(@"Group type %@ and %@",objMessage.groupType,del.communityId); 
    if([objMessage.groupType isEqualToString:@"Personal"]) 
    {   
     [btnSendTo setTitle:[NSString stringWithFormat:@"%@%@",communityName,objMessage.Person] forState: UIControlStateNormal];   
    } 
    else if([objMessage.groupType isEqualToString:@"Everyone"]) 
    {   
     [btnSendTo setTitle:[NSString stringWithFormat:@"%@%@",communityName,@"Everyone"] forState: UIControlStateNormal];  
    } 
    else 
    {   
     [btnSendTo setTitle:[NSString stringWithFormat:@"%@%@",communityName,objMessage.GroupName] forState: UIControlStateNormal];  
    } 


    [btnURL addTarget:self action:@selector(getLink:) forControlEvents:UIControlEventTouchUpInside]; 
    [btnAttachmentName addTarget:self action:@selector(fileDowload:) forControlEvents:UIControlEventTouchUpInside]; 
    [btnAttachment addTarget:self action:@selector(fileDowload:) forControlEvents:UIControlEventTouchUpInside]; 

    backgrounf_View.frame=CGRectMake(24,yAxis,backgroundScroll.frame.size.width-30,oldY_Axis+55); 
    LastMessageYaxis=backgrounf_View.frame.origin.y+backgrounf_View.frame.size.height+10; 

    del.RefreshValue=0; 
    LastValue=index; 

    communityObject *com=[[communityObject alloc]init]; 
    com.MessageId=nil; 
    com.Date=nil; 
    com.Message=nil; 
    com.Link=nil; 
    com.Attachment=nil; 
    com.Filename=nil; 
    com.attachmentType=nil; 
    com.imageSize=nil; 
    com.groupType=nil; 
    com.Person=nil; 
    com.GroupName=nil; 
    com.senderPerson=nil; 
    com.Community=nil; 
    com.inorout=nil; 

    return backgrounf_View;  
} 

Sceenshot is added please check.

+0

也許你應該使用UITableView並從其視圖回收功能中受益。 –

+1

你有沒有用'Instruments' - Allocations/Zombies工具來分析你的應用程序?這可能會讓您瞭解應用程序的內存使用情況。 – Cristik

+0

@jorn Buitink我之前使用的是UItableview,但是需要花費時間來計算height.Its不符合我的要求,所以我刪除了它。我正在實現這個UI – Anupama

回答

0

這一點看起來像陣列的突變而枚舉它:

for (UIView * view in backgroundScroll.subviews) { 
     if (view.tag == 15) { 
      [view removeFromSuperview]; 
     } 
    } 

有難怪爲什麼數組不喜歡它。我也會崩潰:)

順便說一句:它存儲在視圖的標籤數組的索引看起來是非常錯誤的。

+0

如果刪除,我也會得到同樣的錯誤。雖然滾動快速的應用程序崩潰與收到的內存異常 – Anupama

+0

是的。有可能的。我需要通過代碼。但是,這部分代碼在處理帶有標籤15的視圖時是有保證的崩潰。我建議將一些NSAsserts放到關鍵部分。尤其是那些使用索引訪問數組的地方。找出錯誤並不困難。 – Mirek

相關問題