sujith1406
sujith1406

Reputation: 2822

uiimageview gets nil and hence code crashes

in this block of code my uiimageview always get nil.as a result the code crashes.i can t figure out where is the uiimageview getting nil.in this code self refers to uitableviewcell. the code crashes in the code where i am inserting the imageview to the array.is this a problem of memory retain?

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;

Upvotes: 0

Views: 340

Answers (2)

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31720

The issue could be in the below line of code.

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

May be you don't have the an view with tag id 1;

plz check against nil after getting the view from viewWithTag.

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

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

if(self==nil)
        {

what is this.. I think it should be

 if(self!=nil)
        {




and if it is fine then why are you adding subview to a nill

 if(self==nil)
        {

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

Upvotes: 1

Related Questions