custom cells with drawRect: and setNeedsDisplay methods - iphone

there are 3 settings, in my app, that can be change the drawing of a cell.
By default, a cell of my table view show the name and the cost of an object... Changing these 3 setting, an user can choose to show a description or a link insted of cost.
I wrote a lot of code and now, my app can change cell's drawing without quit from it...
My problem is the drawing is changed only for new objects added but old objects don't change!
How can I do to change also the old cell's drawing without quit from app?
This is the code of my cell (i'm using setNeedsDisplay and drawRect methods):
#import "WishTableCell.h"
#implementation WishTableCell
#synthesize wish;
#synthesize imageView;
#synthesize nomeLabel;
#synthesize label;
#synthesize costoLabel;
#synthesize linkDescLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 11, 28, 28)];
imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:imageView];
nomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(58, 8, 235, 22)];
[self.contentView addSubview:nomeLabel];
if ([NSLocalizedString(#"CostoCella", #"") isEqualToString:#"Costo:"]) {
label = [[UILabel alloc] initWithFrame:CGRectMake(58, 28, 40, 16)];
}
else {
label = [[UILabel alloc] initWithFrame:CGRectMake(58, 28, 35, 16)];
}
[self.contentView addSubview:label];
if ([NSLocalizedString(#"CostoCella", #"") isEqualToString:#"Costo:"]) {
costoLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 28, 185, 16)];
}
else {
costoLabel = [[UILabel alloc] initWithFrame:CGRectMake(93, 28, 195, 16)];
}
[self.contentView addSubview:costoLabel];
linkDescLabel = [[UILabel alloc] initWithFrame:CGRectMake(58, 28, 235, 16)];
[self.contentView addSubview:linkDescLabel];
self.backgroundView = [[UIImageView alloc] init];
UIImage *rowBackground = [UIImage imageNamed:#"cellBg.png"];
((UIImageView *)self.backgroundView).image = rowBackground;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)setWish:(Wish *)newWish {
if (newWish != wish) {
[wish release];
wish = [newWish retain];
}
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect {
NSLog(#"DrawRect called!");
nomeLabel.text = wish.nome;
nomeLabel.font = [UIFont boldSystemFontOfSize:18.0];
nomeLabel.textColor = [UIColor colorWithRed:0.039 green:0.4 blue:0.737 alpha:1.0];
nomeLabel.textAlignment = UITextAlignmentLeft;
nomeLabel.shadowColor = [UIColor whiteColor];
nomeLabel.shadowOffset = CGSizeMake(0, 1);
nomeLabel.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:12.0];
label.text = NSLocalizedString(#"CostoCella", #"");
label.textColor = [UIColor colorWithRed:0.262 green:0.258 blue:0.258 alpha:1.0];
label.textAlignment = UITextAlignmentLeft;
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0, 1);
label.backgroundColor = [UIColor clearColor];
costoLabel.font = [UIFont boldSystemFontOfSize:12.0];
costoLabel.textColor = [UIColor colorWithRed:0.262 green:0.258 blue:0.258 alpha:1.0];
costoLabel.textAlignment = UITextAlignmentLeft;
costoLabel.shadowColor = [UIColor whiteColor];
costoLabel.shadowOffset = CGSizeMake(0, 1);
costoLabel.backgroundColor = [UIColor clearColor];
linkDescLabel.font = [UIFont boldSystemFontOfSize:12.0];
linkDescLabel.textColor = [UIColor colorWithRed:0.262 green:0.258 blue:0.258 alpha:1.0];
linkDescLabel.textAlignment = UITextAlignmentLeft;
linkDescLabel.shadowColor = [UIColor whiteColor];
linkDescLabel.shadowOffset = CGSizeMake(0, 1);
linkDescLabel.backgroundColor = [UIColor clearColor];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:#"dettagliView"] isEqualToString:#"costoView"]) {
linkDescLabel.hidden = YES;
label.hidden = NO;
costoLabel.hidden = NO;
if ([[defaults objectForKey:#"valutaCosto"] isEqualToString:#"Euro"]) {
NSString *costo = [[NSString alloc] initWithFormat:#"€ %#", wish.costo];
costoLabel.text = costo;
}
if ([[defaults objectForKey:#"valutaCosto"] isEqualToString:#"Dollaro"]) {
NSString *costo = [[NSString alloc] initWithFormat:#"$ %#", wish.costo];
costoLabel.text = costo;
}
if ([[defaults objectForKey:#"valutaCosto"] isEqualToString:#"Sterlina"]) {
NSString *costo = [[NSString alloc] initWithFormat:#"£ %#", wish.costo];
costoLabel.text = costo;
}
}
else if ([[defaults objectForKey:#"dettagliView"] isEqualToString:#"descrizioneView"]) {
label.hidden = YES;
costoLabel.hidden = YES;
linkDescLabel.hidden = NO;
linkDescLabel.text = wish.descrizione;
}
else if ([[defaults objectForKey:#"dettagliView"] isEqualToString:#"urlView"]) {
label.hidden = YES;
costoLabel.hidden = YES;
linkDescLabel.hidden = NO;
linkDescLabel.text = wish.link;
}
if (wish.categoria == nil)
imageView.image = [UIImage imageNamed:#"personale.png"];
if ([wish.categoria isEqualToString:#"Abbigliamento"])
imageView.image = [UIImage imageNamed:#"abbigliamento.png"];
else if ([wish.categoria isEqualToString:#"Casa"])
imageView.image = [UIImage imageNamed:#"casa.png"];
else if ([wish.categoria isEqualToString:#"Cibo"])
imageView.image = [UIImage imageNamed:#"cibo.png"];
else if ([wish.categoria isEqualToString:#"Divertimento"])
imageView.image = [UIImage imageNamed:#"divertimento.png"];
else if ([wish.categoria isEqualToString:#"Elettronica"])
imageView.image = [UIImage imageNamed:#"elettronica.png"];
else if ([wish.categoria isEqualToString:#"Hobby"])
imageView.image = [UIImage imageNamed:#"hobby.png"];
else if ([wish.categoria isEqualToString:#"Internet"])
imageView.image = [UIImage imageNamed:#"internet.png"];
else if ([wish.categoria isEqualToString:#"Regali"])
imageView.image = [UIImage imageNamed:#"regali.png"];
else if ([wish.categoria isEqualToString:#"Ufficio"])
imageView.image = [UIImage imageNamed:#"ufficio.png"];
else if ([wish.categoria isEqualToString:#"Viaggi"])
imageView.image = [UIImage imageNamed:#"viaggi.png"];
else if ([wish.categoria isEqualToString:#"Personale"])
imageView.image = [UIImage imageNamed:#"personale.png"];
}
- (void)dealloc {
[wish release];
[imageView release];
[nomeLabel release];
[costoLabel release];
[linkDescLabel release];
[label release];
[super dealloc];
}
#end
Thanks a lot for the attention!
Matthew

Try to call [NSTableView reloadData] function, and set the drawing properties in the delegate method (cellForRowAtIndexPath).

I think that you also can use interface to create the cell (.xib), then you do not need write so many assign property code....

Related

Add another view in GMGridView

Hello all,
I have to add one more view in cell of GMGridView. But i am unable to do this because i have to drag my label from view to view1.
My code is :
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView1 cellForItemAtIndex:(NSInteger)index
{
// set size based on orientation
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
if (!cell)
{
cell = [[[GMGridViewCell alloc]init]autorelease];
//one view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
view.backgroundColor = [UIColor redColor];
view.layer.masksToBounds = NO;
view.layer.cornerRadius = 2;
cell.contentView = view;
//another view
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 80, size.width, size.height)];
view1.backgroundColor = [UIColor yellowColor];
view1.layer.masksToBounds = NO;
view1.layer.cornerRadius = 2;
cell.contentView = view1;
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
// allocate label
UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
label.text = (NSString *)[self.currentData objectAtIndex:index];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:20];
[cell.contentView addSubview:label];
return cell;
}
height of cell is 200. but still it shows only one view.
Loading Two types of Cell in GMGridView
This is your Solution, it works as charm..
First Do this Exactly
-(void)prepareExhibitor
{
NSInteger spacing = 1.0;
CGRect rect=self.view.frame;
rect.origin=CGPointMake(0, 0);
self.gmGridView = [[GMGridView alloc] initWithFrame:rect];
self.gmGridView.backgroundColor = [UIColor clearColor];
self.gmGridView.centerGrid=NO;
self.gmGridView.style = GMGridViewStylePush;
self.gmGridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontal];
self.gmGridView.showsHorizontalScrollIndicator=FALSE;
self.gmGridView.clipsToBounds=YES;
self.gmGridView.itemSpacing = spacing;
self.gmGridView.minEdgeInsets = UIEdgeInsetsMake(0,30, 0, 0);
[self.viewNewsHeadline addSubview:self.gmGridView];
self.gmGridView.actionDelegate = self;
self.gmGridView.dataSource = self;
self.gmGridView.mainSuperView = self.superView;
}
Then Write this accordingly as per your Code
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
GMGridViewCell *cell = [gridView dequeueReusableCell];
NewsCell_iPad *view;
UIViewController *controller;
if (!cell)
{
// [self removeGrid];
cell = [[GMGridViewCell alloc] init];
if(index%2==0)
{
controller=[[UIViewController alloc] initWithNibName:#"NewsCellTypeA_iPad" bundle:nil];
}
else
{
controller=[[UIViewController alloc] initWithNibName:#"NewsCellTypeB_iPad" bundle:nil];
}
if(!view)
{
view=(NewsCell_iPad *)controller.view;
}
cell.layer.masksToBounds = NO;
cell.contentView = view;
}
NewsCell_iPad *newsView=(NewsCell_iPad *)cell.contentView;
newsView.news=[self.arrNews objectAtIndex:index];
[self hideGradientBackground:newsView.webViewDetailedNews];
[newsView downloadImage];
[newsView loadWebView];
// NSLog(#"cell=%d ,arr image index=%#",index,[self.arrNews objectAtIndex:index]);
return cell;
}

How to remove duplicated items in iCarousel

https://github.com/nicklockwood/iCarousel/issues/278
I am going to use the iCarousel to show the images linearly.
These images will be gotten dynamically.
Assume that there are 2 images.
But when I update iCarousel, there are also 2 images in background as following image.
While debugging code, I found that viewForItemAtIndex function is called 2 times after I called reloadData function.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
So there are 4 images in iCarousel.
How to prevent to be called 2 times?
Please help me to fix this problem asap.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;
if (view == nil)
{
view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;
puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];
if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:#"puzzle background_small.png"];
puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) ||
((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg.png"];
//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.text = userFriend.m_strName;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
[view addSubview:puzzlePhoto];
[view addSubview:puzzleBG];
[view addSubview:label];
}
return view;
}
The problem is almost certainly due to the views being recycled incorrectly. Views are re-used with different indexes, you you should never set index-specific properties in your view creation code. Here is the correct way to set up your item view:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;
const NSInteger puzzlePhotoTag = 1;
const NSInteger puzzleBGTag = 2;
const NSInteger labelTag = 3;
if (view == nil)
{
//*************************************************
//do setup that is the same for every item view here
//*************************************************
view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;
puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
puzzlePhoto.tag = puzzlePhotoTag;
[view addSubview:puzzlePhoto];
puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
puzzleBG.tag = puzzleBGTag;
[view addSubview:puzzleBG];
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
label.tag = labelTag;
[view addSubview:label];
}
else
{
//get references to subviews
puzzlePhoto = (UIImageView *)[view viewWithTag:puzzlePhotoTag];
puzzleBG = (UIImageView *)[view viewWithTag:puzzleBGTag];
label = (UILabel *)[view viewWithTag:labelTag];
}
//*************************************************
//do setup that is different depending on index here
//*************************************************
//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];
if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:#"puzzle background_small.png"];
if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) || ((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg.png"];
//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label.text = userFriend.m_strName;
return view;
}
As for the scrolling inertia problem, if you're still seeing that after applying this fix, file a separate bug report on the github page.
I solved this problem by performing reloadData on mainThread
[carousel performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:YES];

Subclassing MKPinAnnotationView

I'm creating my own annotation callout by subclassing MKPinAnnotationView. Everything works great, except for when I try to add buttons inside the new callout... the buttons are never called on touch... any idea why?
Thanks in advance!
Code is below:
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.frame = CGRectMake(0.0f, 0.0f, 284.0f, 245.0f);
self.backgroundColor = [UIColor clearColor];
UIImageView *callout_bkg = [[UIImageView alloc] init];
callout_bkg.image = [UIImage imageNamed:#"callout_bkg.png"];
callout_bkg.frame = CGRectMake(0, 0, 240, 110);
[self addSubview:callout_bkg];
titleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 8.0f, 145.0f, 20.0f)];
titleLabel_.textColor = [UIColor whiteColor];
titleLabel_.textAlignment = UITextAlignmentLeft;
titleLabel_.backgroundColor = [UIColor clearColor];
titleLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:12];
[self addSubview:titleLabel_];
descLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 30.0f, 180.0f, 40.0f)];
descLabel_.textColor = [UIColor grayColor];
descLabel_.textAlignment = UITextAlignmentLeft;
descLabel_.backgroundColor = [UIColor clearColor];
descLabel_.numberOfLines = 15;
descLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:10];
[self addSubview:descLabel_];
communityLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(125.0f, 8.0f, 60.0f, 20.0f)];
communityLabel_.textColor = [UIColor whiteColor];
communityLabel_.textAlignment = UITextAlignmentRight;
communityLabel_.backgroundColor = [UIColor clearColor];
communityLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:10];
[self addSubview:communityLabel_];
typeLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 72.0f, 50.0f, 20.0f)];
typeLabel_.textColor = [UIColor whiteColor];
typeLabel_.textAlignment = UITextAlignmentLeft;
typeLabel_.backgroundColor = [UIColor clearColor];
typeLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:10];
[self addSubview:typeLabel_];
facebook_share = [[UIButton alloc] initWithFrame:CGRectMake(200, 17, 29, 27)];
facebook_share.backgroundColor = [UIColor clearColor];
[facebook_share setBackgroundImage:[UIImage imageNamed:#"btn_annotation_share_fb.png"] forState:UIControlStateNormal];
[facebook_share addTarget:self action:#selector(calloutAccessoryTapped) forControlEvents:UIControlStateNormal];
[self addSubview:facebook_share];
twitter_share = [[UIButton alloc] initWithFrame:CGRectMake(200, 44, 29, 28)];
twitter_share.backgroundColor = [UIColor clearColor];
[twitter_share setBackgroundImage:[UIImage imageNamed:#"btn_annotation_share_twitter.png"] forState:UIControlStateNormal];
[twitter_share addTarget:self action:#selector(calloutAccessoryTapped) forControlEvents:UIControlStateNormal];
[self addSubview:twitter_share];
}
return self;
}
-(void)calloutAccessoryTapped {
NSLog(#"TEST!");
}
- (void)dealloc {
[facebook_share release];
[twitter_share release];
[community_ release], community_ = nil;
[communityLabel_ release], communityLabel_ = nil;
[type_ release], type_ = nil;
[typeLabel_ release], typeLabel_ = nil;
[desc_ release], desc_ = nil;
[descLabel_ release], descLabel_ = nil;
[title_ release], title_ = nil;
[titleLabel_ release], titleLabel_ = nil;
[super dealloc];
}
-(void)drawRect:(CGRect)rect {
[super drawRect:rect];
titleLabel_.text = self.type;
descLabel_.text = self.desc;
communityLabel_.text = self.community;
typeLabel_.text = self.title;
[facebook_share addTarget:self action:#selector(test:) forControlEvents:UIControlStateNormal];
}
try with :
facebook_share = [UIButton buttonWithType:UIButtonTypeCustom];
facebook_share.frame = yourFrame;

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;
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];
}
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];
}

Thumbnail grid of images

How does one create a dynamic thumbnail grid?
How is a grid created with thumbnails of images like the photo app on iphone?
How are these spaces arranged dynamically? e.g. adding and removing thumbnails.
this is my simple grid view app
- (void)viewDidAppear:(BOOL)animated {
[[Grid_ViewAppDelegate sharedAppDelegate] hideLoadingView];
temp = temp+1;
if (temp ==1){
NSLog(#"temp:%d",temp);
int n = 20; //Numbers of array;
NSArray *t = [[NSArray alloc] initWithObjects:#"calpie.gif",#"chrysler_electric.png",#"chrysler_fiat_cap.gif",#"cleanup.gif",#"ecylindri.gif",#"elect08.png",#"globe.gif",#"heat.gif",#"insur.gif"
,#"jobs.gif",#"office.gif",#"opec1.gif",#"orng_cap.gif",#"poll.gif",#"pollen.gif",#"purbar.gif",#"robinson.gif",#"robslink_cap.gif",#"scot_cap.gif",#"shoes.gif",nil];
myScrollView.contentSize = CGSizeMake(320, 460+n*2);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 1;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
NSString *mxiURL = #"http://meta-x.com/biflash/images/";
int i =0,i1=0;
while(i<n){
int yy = 4 +i1*79;
for(int j=0; j<4;j++){
if (i>=n) break;
CGRect rect;
rect = CGRectMake(4+79*j, yy, 75, 75);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
NSString *nn = [mxiURL stringByAppendingString:[t objectAtIndex:i]];
UIImage *buttonImageNormal=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: nn]]];
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
button.tag =i;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
//[self.view addSubview:button];
[myScrollView addSubview:button];
//[buttonImageNormal release];
[button release];
i++;
//
}
i1 = i1+1;
//i=i+4;
}
}
}
in AppDelegate
+ (Grid_ViewAppDelegate *)sharedAppDelegate
{
return (Grid_ViewAppDelegate *)[UIApplication sharedApplication].delegate;
}
- (void)showLoadingView
{
if (loadingView == nil)
{
loadingView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
loadingView.opaque = NO;
loadingView.backgroundColor = [UIColor grayColor];
loadingView.alpha = 0.5;
UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(142.0, 222.0, 37.0, 37.0)];
[spinningWheel startAnimating];
spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loadingView addSubview:spinningWheel];
[spinningWheel release];
CGRect label = CGRectMake(142.0f, 255.0f, 71.0f, 20.0f);
lblLoading = [[UILabel alloc] initWithFrame:label];
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont boldSystemFontOfSize:14];
// UILabel
lblLoading.backgroundColor =[UIColor clearColor];
[lblLoading setText:#"Loading..."];
//[lblLoading setTextAlignment:UITextAlignmentCenter];
[loadingView addSubview:lblLoading];
}
[window addSubview:loadingView];
}
- (void)hideLoadingView
{
[loadingView removeFromSuperview];
}
definitely if u apply this logic. u get succeed