How to set Title for cell using Dictionary - iphone

I want to use NSDictionary instead of cell Array.
Following is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
//Add the Bg Image to the cell
//Add the Label
UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(15, 7, 300, 30)];
[cellTitle setBackgroundColor:[UIColor clearColor]];
[cellTitle setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[cellTitle setTextColor:[UIColor darkGrayColor]];
[cellTitle setText:[[cellArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
[cell.contentView addSubview:cellTitle];
return cell;
}

NSDictionary *dictionary1 = [[NSDictionary alloc] initWithObjectsAndKeys:#"ABC",#"Name",#"12",#"Age", nil];
NSDictionary *dictionary2 = [[NSDictionary alloc] initWithObjectsAndKeys:#"DEF",#"Name",#"14",#"Age", nil];
NSDictionary *dictionary3 = [[NSDictionary alloc] initWithObjectsAndKeys:#"GHI",#"Name",#"16",#"Age", nil];
NSDictionary *dictionary4 = [[NSDictionary alloc] initWithObjectsAndKeys:#"JKL",#"Name",#"18",#"Age", nil];
NSArray *array = [[NSArray alloc] initWithObjects:dictionary1,dictionary2,dictionary3,dictionary4, nil];
Now in the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
//Add the Bg Image to the cell
//Add the Label
UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(15, 7, 300, 30)];
[cellTitle setBackgroundColor:[UIColor clearColor]];
[cellTitle setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[cellTitle setTextColor:[UIColor darkGrayColor]];
[cellTitle setText:[[array objectAtIndexPath:indexPath.row] objectForKey:#"Name"]];
[cell.contentView addSubview:cellTitle];
return cell;
}
also in the numberOfRowsInSection:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}

You can use newer syntax like this
NSDictionary *dict = #{#"key" : #"value", #"key2" : #"value2"};
NSDictionary *dict2 = #{#"key" : #"value", #"key2" : #"value2"};
NSArray *array = #(dict, dict2);

Related

Update Data and show it on UITableView iOS

I have a problem: update data in UITableView. I want to get new data from parseXML that response from Server and them update new data to UITableView. **I used beloww code, but it does not show new data on Table View. I wrote a UpdateArray() function to check new data and then I compare 2 Array,if diff [Array count] then I call [tableview reloadData];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [temp count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 90;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
FileNameLabel.backgroundColor = [UIColor clearColor];
FileNameLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
FileNameLabel.textColor = [UIColor blackColor];
NSLog(#"Reseversed TEMP array %#",temp);
FileNameLabel.text =[temp objectAtIndex:indexPath.row];
[cell.contentView addSubview: FileNameLabel];
[FileNameLabel release];
UILabel *UploadTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 25)];
UploadTimeLabel.backgroundColor = [UIColor clearColor];
UploadTimeLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
UploadTimeLabel.textColor = [UIColor grayColor];
UploadTimeLabel.text = [UploadTimeArray objectAtIndex:indexPath.row];
[cell.contentView addSubview: UploadTimeLabel];
[UploadTimeLabel release];
UILabel *CompleteLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 12, 170, 25)];
CompleteLabel.backgroundColor = [UIColor clearColor];
CompleteLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
CompleteLabel.textColor = [UIColor darkGrayColor];
CompleteLabel.text =#"Completed";
CompleteLabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: CompleteLabel];
[CompleteLabel release];
}
return cell;
}
UpdateArray()
-(void)updateArray{
while (loop)
{
[NSThread sleepForTimeInterval:4.0];
[FileCompletedArray removeAllObjects];
// [temp removeAllObjects];
....
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success");
NSString * parsexmlinput = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"Response in Loop CompleteView: %#", parsexmlinput);
// dispatch_async(dispatch_get_main_queue(), ^{
[self parseXMLFile:parsexmlinput];
NSLog(#"File Completed array: %#", FileCompletedArray);
NSLog(#"File Temp out array: %#", temp);
NSLog(#"File Completed count: %lu",(unsigned long)[ FileCompletedArray count]);
NSLog(#"File Temp out count: %lu", (unsigned long)[temp count]);
// NSLog(#"State: %#", state);
if([FileCompletedArray count ] != [temp count])
{
[temp removeAllObjects];
temp= [FileCompletedArray mutableCopy];
[_tableView reloadData];
}
else
{
NSLog(#"2 array equal");
}
//});
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"error: %#", error);
}
];
[httpClient enqueueHTTPRequestOperation:operation];
}
}
Can you help me? Thanks in advance.
I dont See that you call reloadData.
EDIT:
You must check that
1-Temp is having objects i mean that [temp count] is not returning zero.
2-The if condition that checks the two arrays is triggered . I mean that reload data is called.
3-You can make a breakpoint after cell.contetview addsubview and check what the cell contain now?
before you call
[_tableView reloadRowsAtIndexPaths:[_tableView indexPathsForVisibleRows]
withRowAnimation:UITableViewRowAnimationNone];
you have to call
[_tableView beginUpdates];
...
[_tableView endUpdates];
or call
[_tableView reloadData];
If you refresh table view then try this line & check:
[[self mytableview] reloadData];
mytableview is obj of tableview you give here your tableview object.

Alphabetically sorting the contacts inside a Section header in a UItableview

I have an issue of alphabetically sorting the contacts picked from the address book into section headers . I can arrange them alphabetically without the section headers but how to put them according to the names of the contacts ? I do not want to use the NSDictionary as I am able to do the sorting without it. Please view the code below :-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [arrayForLastName count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[alphabets sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrayForLastName count];
}
Any help would be appreciated thanx :)
You can achieve this in following way -
1.Create an index array that contains all the required index -
NSArray *indexArray = [NSArray arrayWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
2.For Each index you need to have an array that will display row for those sections. SO you can have a dictionary that contain an array corresponding to each index.
Check this tutorial, it will help you in implementing this- http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/
Try this code in table delegate methode.
1) - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [dataArray1 count];
}
return number of section in your table view.
2) -(NSMutableArray *)selectedarray:(NSString *)countrynmae
{
NSString *strQuery=[NSString stringWithFormat:#" select * from Globle where Country='%#'",countrynmae];
NSMutableArray *dataarray=[Database executeQuery:strQuery];
return dataarray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *dict =[dataArray1 objectAtIndex:section];
NSString *strcountryname=[dict objectForKey:#"Country"];
NSMutableArray *arr6 = [self selectedarray:strcountryname ];
return [arr6 count];
}
Return number of row in particular section
3) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 44.0)];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.frame = CGRectMake(20.0, -14.0, 300.0, 30.0);
NSDictionary *dict =[dataArray1 objectAtIndex:section];
headerLabel.text=[dict objectForKey:#"Country"];
headerLabel.textColor=[UIColor redColor];
[customView addSubview:headerLabel];
return customView;
}
set header name for section
4) - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.textColor=[UIColor whiteColor];
cell.backgroundColor=[UIColor clearColor];
}
NSDictionary *dict =[dataArray1 objectAtIndex:indexPath.section];
NSString *countryname = [dict objectForKey:#"Country"];
NSString *strQuery = [NSString stringWithFormat:#"Select * from Globle where Country = '%#'",countryname];
NSMutableArray *arr = [Database executeQuery:strQuery];
NSDictionary *dict1 = [arr objectAtIndex:indexPath.row];
cell.textLabel.text=[dict1 objectForKey:#"Name"];
cell.textLabel.numberOfLines=1;
cell.textLabel.textColor=[UIColor blackColor];
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.textAlignment=UITextAlignmentCenter;
NSString *strImage=[dict1 objectForKey:#"ImageName"];
UIImageView *imageView1 = [[UIImageView alloc] init];
UIImage *image1 = [[UIImage alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:strImage ofType:#"jpg"]];
imageView1.image = image1;
imageView1.frame = CGRectMake(6,2,50,42); // position it to the middle
[cell.contentView addSubview:imageView1];
return cell;
[tbl reloadData];
}
return cell in uitable view

Index tableview in iphone sdk

i have an indexed tableview with 8 arrays like
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:#"GEN"];
[tempArray addObject:#"1SA"];
[tempArray addObject:#"EST"];
[tempArray addObject:#"EZE"];
[tempArray addObject:#"NAH"];
[tempArray addObject:#"JOH"];
[tempArray addObject:#"COL"];
[tempArray addObject:#"REV"];
return tempArray;
}
and i get everything right,my problem is when i tap the cell it redirected to the another page with only first array value that is the value inside the [tempArray addObject:#"GEN"];and i tap the values in [tempArray addObject:#"1SA"];etc etc,i get the values inn the [tempArray addObject:#"GEN"];.my DidSelectRowAtIndexPath look like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ChapterSelectionView *detailViewController = [[ChapterSelectionView alloc] initWithNibName:#"ChapterSelectionView" bundle:nil];
//detailViewController.firstString = firstString;
// ...
// Pass the selected object to the new view controller.
detailViewController.selectedIndex=indexPath.row;
detailViewController.selectedCountry = selectedCountry;
appDelegate.selectedBookIndex=indexPath.row;
self.hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
this is my complete tableview code
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return index % 8;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [books count];
}
my viewdidload look like this
- (void)viewDidLoad {
[super viewDidLoad];
l
appDelegate=(Malayalam_BibleAppDelegate *)[[UIApplication sharedApplication] delegate];
s";
books = [[NSMutableArray alloc] init];
NSArray *biblearray1 = [NSArray arrayWithObjects:#"Genesis",
#"Exodus",
#"Leviticus",
#"Numbers",
#"Deuteronomy",
#"Joshua",
#"Judges",
#"Ruth", nil];
NSDictionary *bibledic1 = [NSDictionary dictionaryWithObject:biblearray1 forKey:#"Countries"];
NSArray *biblearray2 = [NSArray arrayWithObjects:#"1Samuel",
#"2Samuel",
#"1King",
#"2King",
#"1Chronicles",
#"2Chronicles",
#"Ezra",
#"Nehemiah", nil];
......etc etc
[books addObject:bibledic1];
[books addObject:bibledic2];
.....etc etc
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *dictionary = [books objectAtIndex:section];
NSArray *array = [dictionary objectForKey:#"Countries"];
return [array 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] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor brownColor];
// [myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
// Configure the cell.
// cell.textLabel.tag =row*1+col;
//First get the dictionary object
NSDictionary *dictionary = [books objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Countries"];
//NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text =[array objectAtIndex:indexPath.row];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
//cell.textLabel.text = [books objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;;
return cell;
}
how to get the correct values from tableview cell.
Thanks in advance.
EDIT
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationController] setNavigationBarHidden:YES animated:NO];
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,49,320,480)];
appDelegate = (Malayalam_BibleAppDelegate *)[[UIApplication sharedApplication] delegate];
//self.navigationItem.title=[appDelegate.books objectAtIndex:selectedIndex];
chapterlabel.text = [appDelegate.books objectAtIndex:selectedIndex];
buttonArray =[[NSMutableArray alloc]initWithCapacity:0];
//self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"bg10"]];
[self.view addSubview:scrollView];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[self navigationController] setNavigationBarHidden:YES animated:NO];
n=[DbHandler mNumberOfChaptersInBook:[appDelegate.books objectAtIndex:selectedIndex]];
int scrollViewHieght=n/6;
scrollView.contentSize = CGSizeMake(320,10+34*scrollViewHieght);
i=1;
int rowCount=n/6;
for(int row=0;row<=rowCount;row++){
for (int col = 0; col < 6; col++) {
if(i<=n){
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.textColor=[UIColor blueColor];
button.titleLabel.font = [UIFont fontWithName:#"Georgia" size:15.0];
[button setBackgroundImage:[UIImage imageNamed:#"tabs"] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//button.frame=CGRectMake(col*52+5,row*34+50,50,32);
button.frame=CGRectMake(col*52+5,row*34+0,50,32);
[button addTarget:self action:#selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"%d",i++] forState:UIControlStateNormal];
//button.titleLabel.font=[UIFont boldSystemFontOfSize:15];
button.titleLabel.textColor=[UIColor blackColor];
button.tag =row*6+col;
[buttonArray addObject:button];
[scrollView addSubview:[buttonArray objectAtIndex:row*6+col]];
//[self.view addSubview:button];
[button release];
}
}
}
}
you also need to send section number to the detailViewController. Depending on the section you have to select the required array.

iphone: problem reloading table data

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.

how do we access values stored in NSMutableArray of NSMutableDictionary?

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.