hi i am working on UITableView custom cell for twitter, for each cell i am adding UIImageView(userPhoto), UILabel(name), UILabel(tweet), UILabel(Data), This i have to dealloc all elements,
please go through the attached Image of INSTRUMENT TOOL alloc screen shot
when i run the Instrument tool, it showing some are not dealloc, how to dealloc,
this is my code
//Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:nil];
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(72,3,242,15)] ;
name.text = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] userName];
[name setFont:[UIFont fontWithName:#"Helvetica" size:13]];
name.textColor=[UIColor colorWithRed:250 green:250 blue:210 alpha:0.5];
[name setBackgroundColor:[UIColor clearColor]];
UILabel *date = [[[UILabel alloc]initWithFrame:CGRectMake(200,3,130,15)] autorelease];
date.text = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] created_at];
[date setFont:[UIFont fontWithName:#"Helvetica" size:12]];
date.textColor=[UIColor colorWithRed:250 green:250 blue:210 alpha:0.5];
[date setBackgroundColor:[UIColor clearColor]];
UILabel * tweetLabel = [[UILabel alloc]initWithFrame:CGRectMake(72,20,242,60)] ;
tweetLabel.text = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] tweet];
tweetLabel.numberOfLines = 3;
tweetLabel.textColor=[UIColor colorWithRed:252 green:148 blue:31 alpha:1];
[tweetLabel setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[tweetLabel setBackgroundColor:[UIColor clearColor]];
UIImageView *myImage = [[UIImageView alloc]initWithFrame:CGRectMake(6,10,58,60)] ;
NSURL *url = [NSURL URLWithString:[(Tweet*)[tweetArray objectAtIndex:indexPath.row] image_url]];
NSData *data = [NSData dataWithContentsOfURL:url];
[myImage setImage: [UIImage imageWithData:data]];
[cell.contentView addSubview:myImage];
[cell.contentView addSubview:tweetLabel];
[cell.contentView addSubview:name];
[cell.contentView addSubview:date];
[myTweetActivityIndicator stopAnimating];
[myTweetActivityIndicator setHidesWhenStopped:YES];
return cell;
}
please guide me
Thank you
You can use
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:nil] autorelease];
For other labels, you can probably use
[date release]; [tweetLabel release]; [name release]; [myImage release];
You'll have to send a release message to all the objects that you have allocated using init/alloc (name, date, tweetLabel, myImage) as soon as you're finished with them (for example, after addSubview). Note that according to the documentation, the view is retained by the receiver.
try using
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
[myImage setImage: [UIImage imageWithData:data]];
[data release];
data = nil;
In this case, we are manually releasing the memory allocated. I hope, this works
Related
As TableView cell labels are displayed on load but when the tableView is scrolled the tableview contents are not appearing. But if I select any of the cell then the labels in that particular cell are displaying.
Please help me. I have the same problem in different aspect too. I did not get any resolution for it.
Here is my code of cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = #"newsTableCell";
UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
user.tag = 101;
[cell.contentView addSubview:user];
[user release];
UILabel *usr = (UILabel *)[cell viewWithTag:101];
usr.font = [UIFont boldSystemFontOfSize:12.0];
[usr setText:[appDelegate.usernames objectAtIndex:[indexPath row]]];
UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
status.tag = 102;
[cell.contentView addSubview:status];
[status release];
status = (UILabel *)[cell viewWithTag:102];
status.font = [UIFont boldSystemFontOfSize:12.0];
[status setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
UILabel *rival = [[UILabel alloc] initWithFrame:CGRectMake(220,10,80,21)];
rival.tag = 103;
[cell.contentView addSubview:rival];
[rival release];
UILabel *rivals = (UILabel *)[cell viewWithTag:103];
rivals.font = [UIFont boldSystemFontOfSize:12.0];
[rivals setText:[appDelegate.rivalusernames objectAtIndex:[indexPath row]]];
UILabel *gamename = [[UILabel alloc] initWithFrame:CGRectMake(84,27,208,21)];
gamename.tag = 104;
[cell.contentView addSubview:gamename];
[gamename release];
UILabel *gamenames = (UILabel *)[cell viewWithTag:104];
gamenames.font = [UIFont boldSystemFontOfSize:12.0];
[gamenames setText:[appDelegate.challengenames objectAtIndex:[indexPath row]]];
UILabel *time = [[UILabel alloc] initWithFrame: CGRectMake(84,47,149,21)];
time.tag = 105;
[cell.contentView addSubview:time];
[time release];
UILabel *times = (UILabel *)[cell viewWithTag:105];
times.font = [UIFont boldSystemFontOfSize:12.0];
[times setText:[appDelegate.remainingtime objectAtIndex:[indexPath row]]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,10,67,58)];
imageView.tag = 106;
[cell.contentView addSubview:imageView];
[imageView release];
NSURL *url = [NSURL URLWithString:[appDelegate.imagepaths objectAtIndex:[indexPath row]]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imgView = (UIImageView *)[cell viewWithTag:106];
imgView.image = img;
return cell;
}
first of all move these lines inside the if (cell==NIL):
if(cell == nil) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
user.tag = 101;
[cell.contentView addSubview:user];
[user release];
}
or you add a subview to the cell each time you reuse it, scrolling
and the same for:
UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
status.tag = 102;
[cell.contentView addSubview:status];
[status release];
and
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,10,67,58)];
imageView.tag = 106;
[cell.contentView addSubview:imageView];
[imageView release];
...etc etc
as a general rule: you add/alloc new objects just once when you first create a cell (inside the if (cell==nil))
then outside that if you just reuse all objects and change their properties, as text of image source...
Maybe the color of your UILabels is the same as the background color? When you select a cell and then you can see the text, it probably is because of the then highlighted cell color ...
And I also strongly suggest you make a cell subclass as cortez mentioned, this code is not read- nor maintainable ...
your implementation of the UITableView Cell is rong try this way
i Know your problem will solve.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = #"newsTableCell";
UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
user.tag = 101;
usr.font = [UIFont boldSystemFontOfSize:12.0];
[cell.contentView addSubview:user];
[user release];
UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
status.tag = 102;
status.font = [UIFont boldSystemFontOfSize:12.0];
[cell.contentView addSubview:status];
[status release];
UILabel *rival = [[UILabel alloc] initWithFrame:CGRectMake(220,10,80,21)];
rival.tag = 103;
[cell.contentView addSubview:rival];
[rival release];
}
else
{
UILabel *usr = (UILabel *)[cell viewWithTag:101];
status = (UILabel *)[cell viewWithTag:102];
rival = (UILabel *)[cell viewWithTag:103];
}
[usr setText:[appDelegate.usernames objectAtIndex:[indexPath row]]];
[status setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
[rival setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
return cell;
}
Initially I written the code as above. After the suggestion of meronix I have changed to below one. Please suggest.
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if (tableView.tag ==16)
{
NSLog(#"%d",appDelegate.checkChallenges);
static NSString *CellIdentifier = #"newsTableCell";
UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
user.tag = 101;
[cell.contentView addSubview:user];
[user release];
UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
status.tag = 102;
[cell.contentView addSubview:status];
[status release];
UILabel *rival = [[UILabel alloc] initWithFrame:CGRectMake(220,10,80,21)];
rival.tag = 103;
[cell.contentView addSubview:rival];
//[rival release];
UILabel *gamename = [[UILabel alloc] initWithFrame:CGRectMake(84,27,208,21)];
gamename.tag = 104;
[cell.contentView addSubview:gamename];
[gamename release];
UILabel *time = [[UILabel alloc] initWithFrame: CGRectMake(84,47,149,21)];
time.tag = 105;
[cell.contentView addSubview:time];
[time release];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,10,67,58)];
imageView.tag = 106;
[cell.contentView addSubview:imageView];
[imageView release];
}
UILabel *usr = (UILabel *)[cell viewWithTag:101];
usr.font = [UIFont boldSystemFontOfSize:12.0];
[usr setText:[appDelegate.usernames objectAtIndex:[indexPath row]]];
UILabel *stat = (UILabel *)[cell viewWithTag:102];
stat.font = [UIFont boldSystemFontOfSize:12.0];
[stat setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
NSURL *url = [NSURL URLWithString:[appDelegate.imagepaths objectAtIndex:[indexPath row]]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imgView = (UIImageView *)[cell viewWithTag:106];
imgView.image = img;
UILabel *times = (UILabel *)[cell viewWithTag:105];
times.font = [UIFont boldSystemFontOfSize:12.0];
[times setText:[appDelegate.remainingtime objectAtIndex:[indexPath row]]];
UILabel *gamenames = (UILabel *)[cell viewWithTag:104];
gamenames.font = [UIFont boldSystemFontOfSize:12.0];
[gamenames setText:[appDelegate.challengenames objectAtIndex:[indexPath row]]];
UILabel *rivals = (UILabel *)[cell viewWithTag:103];
rivals.font = [UIFont boldSystemFontOfSize:12.0];
[rivals setText:[appDelegate.rivalusernames objectAtIndex:[indexPath row]]];
return cell;
}
I need to laod a image from NSMutableArray which is loaded from service url.I can retrieve the image tag from XML response.Now I want that image in a UITableViewCell
My array looks like this :
(
{
Image="46b6daca-3.png";
}
{
Image="9251bfe5-d.jpg";
}
)
How can I load each image in a seperate UITableViewCell ?
For creating a UILabel I knew it is like this :
NSMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row];
UILabel *line1=[[UILabel alloc]initWithFrame:CGRectMake(10, 68, 320,10)];
line1 .font=[UIFont fontWithName:#"arial" size:12];
[line1 setTextAlignment:UITextAlignmentLeft];
[line1 setText:[d valueForKey:#"Name"]];
line1.tag=113;
[cell addSubview:line1 ];
[line1 release];
How can I do it for UIImage?
UIImageView *img = [[UIImageView alloc]initWithImage:[ d objectForKey:#"Image"]];
[cell.contentView addSubView:img];
as per you code.. try this.
NSMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row];
UIImageView *img = [[UIImageView alloc]initWithImage:[ d objectForKey:#"Image"]];
img,frame = CGRectMake("Set frame")]
[cell.contentView addSubView:img];
[img release];
if you have images in your array then use direct array value as a image like bellow..
UIImageView *img = [[UIImageView alloc]initWithImage:[[arrData valueForKey:#"Image"] objectAtIndex:indexPath.row];
img.frame = SetFrameHere;//
[cell.contentView addSubView:img];
or also add as a cell imageview like bellow..
cell.imageView.image = (UIImage *)[[arrData valueForKey:#"Image"] objectAtIndex:indexPath.row];
Use the following code for setting the image:
UIImage *img = [[UIImage imageNamed:[d objectForKey:#"Image"];
cell.imageView.image = img;
Here d is your NSMutableDictionary
I have an application which is working fine.But i need to be memory leaks free.So i decided to build with analyzer,then i saw so many small small memory management issues.I sorted out almost everything except this.here my cell object is shown as a leaked object,but really i don't know where i was wrong footed.this is my cellforRowatIndexpath method .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:EventCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier];
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"cell_with_arrow.png"]];
[cell setBackgroundView:img];
[img release];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:13.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; // Pre-iOS6 use UILineBreakModeWordWrap
cell.textLabel.numberOfLines =0;
[cell.textLabel sizeToFit];
}
[[cell viewWithTag:12] removeFromSuperview];
[[cell viewWithTag:13] removeFromSuperview];
PTPusherEvent *event = [eventsReceived objectAtIndex:indexPath.row];
//cell.textLabel.text = event.name;
// cell.detailTextLabel.text = [event.data description];
NSDictionary *payload =event.data;
NSLog(#"%#",payload);
NSString *tes=[payload objectForKey:#"from"];
NSString *subtitle = [NSString stringWithString: #"From "];
subtitle = [subtitle stringByAppendingFormat:#"%#",tes];
cell.detailTextLabel.text = subtitle;
cell.textLabel.text = [payload objectForKey:#"message"];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// [dateFormat setDateFormat:#"dd-MMM-YYYY HH:mm:ss"];
[dateFormat setDateFormat:#"dd-MMM-YYYY hh:mm a"];
[dateFormat setTimeZone:[NSTimeZone defaultTimeZone]];
NSDate *todaysdate=[NSDate date];
NSString *todaysdateString = [dateFormat stringFromDate:todaysdate];
[dateFormat release];
CGSize boundingSize = CGSizeMake(320, CGFLOAT_MAX);
CGSize requiredSize = [cell.textLabel.text sizeWithFont:cell.textLabel.font
constrainedToSize:boundingSize
lineBreakMode:UILineBreakModeWordWrap];
CGFloat requiredHeight = requiredSize.height;
MarqueeLabel *rateLabelOne = [[MarqueeLabel alloc] initWithFrame:CGRectMake(115, requiredHeight+15,155, 20) rate:50.0f andFadeLength:10.0f];
rateLabelOne.numberOfLines = 1;
rateLabelOne.marqueeType = MLContinuous;
rateLabelOne.opaque = NO;
rateLabelOne.enabled = YES;
rateLabelOne.shadowOffset = CGSizeMake(0.0, -1.0);
rateLabelOne.textAlignment = UITextAlignmentRight;
rateLabelOne.textColor = [UIColor redColor];
rateLabelOne.backgroundColor = [UIColor clearColor];
rateLabelOne.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.000];
rateLabelOne.text = #"OFFline!cannot communicate now.....";
MarqueeLabel *rateLabelTwo = [[MarqueeLabel alloc] initWithFrame:CGRectMake(215, requiredHeight+15,100, 20) rate:50.0f andFadeLength:10.0f];
rateLabelTwo.marqueeType = MLContinuous;
rateLabelTwo.numberOfLines = 1;
rateLabelTwo.opaque = NO;
rateLabelTwo.enabled = YES;
rateLabelTwo.shadowOffset = CGSizeMake(0.0, -1.0);
rateLabelTwo.textAlignment = UITextAlignmentRight;
rateLabelTwo.textColor = [UIColor blueColor];
rateLabelTwo.backgroundColor = [UIColor clearColor];
rateLabelTwo.font = [UIFont fontWithName:#"Helvetica" size:12.000];
rateLabelTwo.text = todaysdateString;
if([chatstatus isEqualToString:#"online"])
{
rateLabelTwo.tag=12;
[cell addSubview:rateLabelTwo];
[rateLabelTwo release];
}
else
{
rateLabelOne.tag=13;
[cell addSubview:rateLabelOne];
[rateLabelOne release];
}
return cell;
}
Can anybody help me to find out where is that monster?
You need to put an autorelease as,
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier] autorelease];
Otherwise it will be leaked.
Update:
As mentioned by AppleDelegate,
[rateLabelTwo release]; and [rateLabelOne release]; also need to be moved out of the if condition so that it will be released in both if and else cases. Analyzer will detect this also as a leak.
OK here's where the issue lies..
if([chatstatus isEqualToString:#"online"])
{
rateLabelTwo.tag=12;
[cell addSubview:rateLabelTwo];
[rateLabelTwo release];
}
else
{
rateLabelOne.tag=13;
[cell addSubview:rateLabelOne];
[rateLabelOne release];
}
In your code the rateLabelOne and rateLabelTow are released within an if loop.The static analyzer analyses it as a leak since it assumes there might be a case where the code doesnt run inside the if loop.Practically its not a leak.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier]autoRelease];
Use this line:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EventCellIdentifier]autorelease];
This will remove your memory leak.
I am using custom label in table cell.everytime i visit again this page the label text getting more darker like it is behaving overwriting.
how can i fix this?
- (void)viewWillAppear:(BOOL)animated {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Note" inManagedObjectContext:context];
[request setEntity:entity];
[request release];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
Note *noteItem = [resultController objectAtIndexPath:indexPath];
//[cell.textLabel setText:[noteItem noteTitle]];
//[cell.detailTextLabel setText:[dateFormatter stringFromDate:[noteItem creationDate]]];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 19)];
newLabel.text = [noteItem noteTitle];
[newLabel setBackgroundColor:[UIColor clearColor]];
[cell addSubview:newLabel];
[newLabel release];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 20, 200, 26)];
detailLabel.text = [dateFormatter stringFromDate:[noteItem creationDate]];
[detailLabel setFont:[UIFont fontWithName:#"Helvetica" size:12.0]];
[detailLabel setBackgroundColor:[UIColor clearColor]];
[cell addSubview:detailLabel];
[detailLabel release];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setAlpha:0.6];
return cell;
}
Create UILabel in
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
and Assign Its Value outside of this condition
your cellForRowAtIndexPath method should look like-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
nameLabelFrame = CGRectMake(5, 5, 200, 19);
countLabelFrame = CGRectMake(5, 20, 200, 26);
UILabel *lblTemp;
lblTemp = [[UILabel alloc] initWithFrame:nameLabelFrame];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
lblTemp = [[UILabel alloc] initWithFrame:countLabelFrame];
lblTemp.tag = 2;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
}
// Set up the cell...
Note *noteItem = [resultController objectAtIndexPath:indexPath];
UILabel *newLabel = (UILabel *)[cell viewWithTag:1];
newLabel.text = [noteItem noteTitle];
[newLabel setBackgroundColor:[UIColor clearColor]];
UILabel *detailLabel = (UILabel *)[cell viewWithTag:2];
detailLabel.text = [dateFormatter stringFromDate:[noteItem creationDate]];
[detailLabel setFont:[UIFont fontWithName:#"Helvetica" size:12.0]];
[detailLabel setBackgroundColor:[UIColor clearColor]];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setAlpha:0.6];
return cell;
}
You should check if the cell == nil. If so, then add all the labels again, otherwise, they have already been added.
I have stored values in NsMutableDictionaries . ThenI stored all the dictionaries in NSMutable Array. I need to access the values ? How can I do that ?
-(void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Library";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleBordered target:self action:#selector(close:)];
cells = [[NSMutableArray alloc] initWithObjects:#"dict1", #"dict2", #"dict3", #"dict4", #"dict5", #"dict6", nil];
dict1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Mon, 01 Feb #2", #"date", #"0.7", #"time", #"1.2MB", #"size", #"200*200", #"pix", nil];
dict2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Wed, 02 Mar #3", #"date", #"1.2", #"time", #"2.2MB", #"size", #"300*300", #"pix", nil];
dict3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Tue, 03 Apr #5", #"date", #"1.7", #"time", #"2.5MB", #"size", #"240*240", #"pix", nil];
dict4 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Mon, 01 Feb #2", #"date", #"0.7", #"time", #"1.2MB", #"size", #"200*200", #"pix", nil];
dict5 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Mon, 10 Nov #5", #"date", #"2.7", #"time", #"4.2MB", #"size", #"200*400", #"pix", nil];
dict6 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Mon, 11 Dec #6", #"date", #"4.7", #"time", #"2.2MB", #"size", #"500*200", #"pix", nil];
//[cells addObject:dict1];
//[cells addObject:dict2];
//[cells addObject:dict3];
//[cells addObject:dict4];
//[cells addObject:dict5];
//[cells addObject:dict6];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cells count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{ cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//cell.contentView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 80.0f);
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
UIImageView *image1 = [[UIImageView alloc]init];
image1.frame = CGRectMake(0.0f, 0.0f, 80.0f, 80.0f);
image1.tag = tag7;
UILabel *dateLabel = [[UILabel alloc]init];
dateLabel.frame = CGRectMake(100.0f, 5.0f, 120.0f, 25.0f);
dateLabel.font = [UIFont fontWithName:#"Georgia" size:10];
dateLabel.tag = tag1;
UILabel *timeLabel = [[UILabel alloc] init];
timeLabel.frame = CGRectMake(100.0f, 30.0f, 40.0f, 25.0f);
timeLabel.font = [UIFont fontWithName:#"Georgia" size:10];
timeLabel.tag = tag2;
UILabel *sizeLabel = [[UILabel alloc] init];
sizeLabel.frame = CGRectMake(160.0f, 30.0f, 40.0f, 25.0f);
sizeLabel.font = [UIFont fontWithName:#"Georgia" size:10];
sizeLabel.tag = tag3;
UILabel *pixLabel = [[UILabel alloc] init];
pixLabel.frame = CGRectMake(220.0f, 30.0f, 40.0f, 25.0f);
pixLabel.font = [UIFont fontWithName:#"Georgia" size:10];
pixLabel.tag = tag4;
UILabel *shareLabel = [[UILabel alloc] init];
shareLabel.frame = CGRectMake(100.0f, 55.0f, 100.0f, 25.0f);
shareLabel.font = [UIFont fontWithName:#"Georgia" size:10];
shareLabel.tag = tag5;
UILabel *deleteLabel = [[UILabel alloc] init];
deleteLabel.frame = CGRectMake(220.0f, 55.0f, 100.0f, 25.0f);
deleteLabel.font = [UIFont fontWithName:#"Georgia" size:10];
deleteLabel.tag = tag6;
[cell.contentView addSubview:dateLabel];
[cell.contentView addSubview:timeLabel];
[cell.contentView addSubview:sizeLabel];
[cell.contentView addSubview:pixLabel];
[cell.contentView addSubview:shareLabel];
[cell.contentView addSubview:deleteLabel];
[cell.contentView addSubview:image1];
[dateLabel release];
[timeLabel release];
[sizeLabel release];
[pixLabel release];
[shareLabel release];
[deleteLabel release];
[image1 release];
}
// Set up the cell...
[(UILabel *)[cell viewWithTag:tag1] setText:[cells objectAtIndex:[dict1 objectForKey: #"date"]]];
[(UILabel *)[cell viewWithTag:tag2] setText:[cells objectAtIndex:[dict1 objectForKey: #"time"]]];
[(UILabel *)[cell viewWithTag:tag3] setText:[cells objectAtIndex:[dict1 objectForKey: #"size"]]];
[(UILabel *)[cell viewWithTag:tag4] setText:[cells objectAtIndex:[dict1 objectForKey: #"pix"]]];
[(UILabel *)[cell viewWithTag:tag5] setText:#"Share"];
[(UILabel *)[cell viewWithTag:tag6] setText:#"Delete"];
cell.imageView.image = [UIImage imageNamed:#"image2.png"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80.0f;
}
I did in above way but it is not working. I know the mistake is at the accessing values. but, I could not get how to do it ?
Thank You.
You haven't stored the dictionaries at all—just the strings "dict1", "dict2", "dict3", and so on. The array initializer you're using should be something like
cells = [[NSMutableArray alloc] initWithCapacity:6];
I'm not sure why you've got all of the [cells addObject:dictionaryN]; lines commented out, because that's the correct way to add the dictionaries to the array; you also need to have a [dictionaryN release]; after each of them to prevent memory leaks.
To get the values out of the dictionaries in the array, you need to do something like this in your -tableView:cellForRowAtIndexPath: method:
NSDictionary *rowDictionary = [cells objectAtIndex:indexPath.row];
[(UILabel *)[cell viewWithTag:tag1] setText:[rowDictionary objectForKey:#"date"]];
[(UILabel *)[cell viewWithTag:tag2] setText:[rowDictionary objectForKey:#"time"]];
// etc.