2011-05-13 120 views
0

在這個代碼塊我的uiimageview總是得到nil.as結果代碼崩潰。我無法弄清楚uiimageview在哪裏得到nil.in此代碼自我指的是uitableviewcell。 該代碼崩潰的代碼中,我插入imageview到array.is這是一個內存保留問題?uiimageview得到零,因此代碼崩潰

MyAppDelegate *appDelegate =(myAppDelegate *)[[UIApplication sharedApplication]delegate]; 
     UIImageView *profileImageView; 
     UILabel *tweetLabel; 
     UILabel *timeLabel; 
     UILabel *profileNameLabel; 
     UIView *message; 
     if(self==nil) 
     { 


     profileImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; 
     profileImageView.backgroundColor= [UIColor clearColor]; 
     profileImageView.tag = 1; 

     tweetLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     tweetLabel.backgroundColor = [UIColor clearColor]; 
     tweetLabel.tag = 2; 
     tweetLabel.numberOfLines = 3; 
     tweetLabel.lineBreakMode = UILineBreakModeWordWrap; 
     tweetLabel.font = [UIFont systemFontOfSize:10.0]; 
     tweetLabel.textColor=[UIColor blackColor]; 

     timeLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     timeLabel.backgroundColor = [UIColor clearColor]; 
     timeLabel.tag = 3; 
     timeLabel.numberOfLines = 1; 
     timeLabel.lineBreakMode = UILineBreakModeWordWrap; 
     timeLabel.font = [UIFont systemFontOfSize:12.0]; 
     timeLabel.textColor=[UIColor blackColor]; 

     profileNameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     profileNameLabel.backgroundColor = [UIColor clearColor]; 
     profileNameLabel.tag = 4; 
     profileNameLabel.numberOfLines = 1; 
     profileNameLabel.lineBreakMode = UILineBreakModeWordWrap; 
     profileNameLabel.font = [UIFont boldSystemFontOfSize:14.0]; 
     profileNameLabel.textColor=[UIColor blackColor]; 

     message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height)]; 
     message.tag = 0; 
     [message addSubview:profileImageView]; 
     [message addSubview:tweetLabel]; 
     [message addSubview:timeLabel]; 
     [message addSubview:profileNameLabel]; 

     [self addSubview:message]; 
     } 
     else{ 
      tweetLabel = (UILabel *)[[self.contentView viewWithTag:0]viewWithTag:2]; 
      timeLabel = (UILabel *)[[self.contentView viewWithTag:0]viewWithTag:3]; 
      profileNameLabel = (UILabel *)[[self.contentView viewWithTag:0]viewWithTag:4]; 

     profileImageView = (UIImageView *)[self.contentView viewWithTag:1]; 
     NSLog(@" profileImageView %@",profileImageView); 
     } 
     NSString *tweet=[tweetObject tweet]; 
     NSString *profileName=[tweetObject author]; 
     NSString *time=[tweetObject time]; 



     CGSize textSize = [tweet sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap]; 
     CGSize timeSize = [time sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap]; 
     CGSize profileNameSize = [profileName sizeWithFont:[UIFont boldSystemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap]; 

     profileImageView.frame = CGRectMake(5,self.frame.size.height/2, 44, 44); 
     NSLog(@" profileImageView %@",profileImageView); 
      tweetLabel.frame = CGRectMake(54, profileImageView.frame.size.width, 240,25); 

     timeLabel.frame = CGRectMake(180, 5.0f, 100, timeSize.height); 

     profileNameLabel.frame = CGRectMake(54,5, 150, profileNameSize.height); 
     tweetLabel.text=tweet; 
     timeLabel.text=time; 
     profileNameLabel.text=profileName; 


     profileImageView.image = [[UIImage imageNamed:@"fbdefaultProfile.gif"]retain]; 
     NSLog(@" myArray WILL FILL WITH THIS ELEMNTS %@ %@ %@ ",profileImageView,[tweetObject imageURL],@"fbdefaultProfile.gif"); 
     NSArray *myArray = [NSArray arrayWithObjects:profileImageView,[tweetObject imageURL],@"fbdefaultProfile.gif",nil]; 
     NSLog(@" myArray %@",myArray); 
     NSLog(@"%@ myArray ",myArray); 
     [appDelegate performSelectorInBackground:@selector(updateImageViewInBackground:) withObject:myArray]; 

     } 
     return; 

回答

1
if(self==nil) 
     { 

這是什麼..我想應該是

if(self!=nil) 
     { 




,如果它是好的,爲什麼要補充子視圖到nill

if(self==nil) 
     { 

     //your code ..... 
     //and then you are adding sub view to a nil which is useless 
     [self addSubview:message]; 
     } 
+0

三江源此指出錯誤。其實在條件right.i不得不增加這僅在細胞nil.otherwise它WUD繼續添加子視圖如果u滾動tableview.only的事情是,我錯過了創建一個tableviewcell是它nil.thanks – sujith1406 2011-05-13 06:49:23

+0

這是好事... :) – 2011-05-13 06:55:42

0

該問題可能出現在下面的代碼行中。

profileImageView = (UIImageView *)[self.contentView viewWithTag:1]; 

可能是你沒有標籤id爲1的視圖;

viewWithTag獲取視圖後,對零檢查。

if([self.contentView viewWithTag:1] != nil) 
{ 
    profileImageView = (UIImageView *)[self.contentView viewWithTag:1]; 
}