Load UITableView with NSArray - iphone

I have an NSArray of NSDictionaries where each dictionary has the same keys. I want a section for each dictionary in the array.
For example, if the array has 4 dictionaries, then the table will have 4 sections. Then each cell in each section will correspond with that value in the dictionary.
Is there an easy way to do this?
For example, I have an NSArray with 3 Dictionaries where each dictionary has 3 keys, name, gender, and date.
This means that the table should have 3 sections, where each section title is the date key for its dictionary. Then there are 2 cells for that section corresponding to the name and gender values.

Ya I'm familar with those methods, but I'm not sure how to get the
date key from the dictionary correctly setup in the
titleForHeaderInSection method.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return (NSString *)[myArray objectAtIndex:section] objectForKey:#"date"];
}
This worked flawlessly. But how can I edit the code in cellForRowAtIndexPath in a similar manner?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/* Create myCell UITableViewCell */
NSString *value = #"Unknown";
if (indexPath.row == 0){
value = (NSString *)[[myArray objectAtIndex:indexPath.section] objectForKey:"name"];
}else if (indexPath.row == 1){
value = (NSString *)[[myArray objectAtIndex:indexPath.section] objectForKey:#"gender"];
}
myCell.textPropertyOrWhatever = value;
return myCell;
}
Are you even trying this at all? :P

Related

How to get a UITableViewCell's subtitle show how many times that specific cell was tapped?

I have a uitableview that displays the values of an array. I would like to know if there is a way to update the subtitle of its table cells, based on how many times each cell was tapped.
Thank you!
First of all, you'll want to use a NSMutableArray so you can change its contents after instantiation. Here's a basic over view of what I just tried to achieve your intended results:
In your interface
#property (strong, nonatomic) NSMutableArray *tapCountArray;
In your implementation
- (void)viewDidLoad
{
[super viewDidLoad];
self.tapCountArray = [NSMutableArray new];
int numberOfRows = 20;
for (int i = 0; i < numberOfRows; i ++) {
[self.tapCountArray addObject:#(0)];
}
}
Then the important part!
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tapCountArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *text = [self.tapCountArray[indexPath.row] stringValue];
[cell.textLabel setText:text];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tapCountArray replaceObjectAtIndex:indexPath.row withObject:#([self.tapCountArray[indexPath.row] intValue] + 1)];
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
When each cell is tapped the number in its detailTextLabel will be incremented up by one.
You shouldn't create a new array or set, that could lead to problems if the two arrays get out of sync with each other. The way to do it, as you suggested in your comment, is to use dictionaries. The way you said you were doing that is probably not the way, however. You want an array of dictionaries where the values for one key would be whatever your main data is and the value for the other key would be the number of taps. For example, lets call the two keys main and sub, and your main data is a set of names. The array of dictionaries would look like this: ({main:#"Tom",sub:1}, {main:#"Dick", sub:0}, {main:#"Harry",sub:2}, .....). In the tableView:cellForRowAtIndexPath:indexPath method you would provide the data to the cells like this:
cell.textLabel.text = [[array objectAtIndex:indexPath.row] valueForKey:#"main"];
cell.detailTextLabel.text = [[array objectAtIndex:indexPath.row] valueForKey:#"sub"];
I think you can just set up another array of the same length as the one you have now. Then when your didSelectRowAtIndexPath is triggered, increment your indexPath.row entry of the new array and refresh that cell. If you don't expect to shuffle the table, you don't need a dictionary.
You can insert the object into an NSCountedSet, and on your cellForRowAtIndexPath method, you would take the model object for the cell and verify the number of times it has been inserted into the NSCountedSet instance.
Take a look at the NSCountedSet documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSCountedSet_Class/Reference/Reference.html

Load more cells problem

i use MWFeedParser library to parse xml.
when i change the numberOfRowsInSection to +1 (return [itemsToDisplay count] +1;)
to make the last cell row for the Load more option my app crash in this line:
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
any idea why this happens?
wel you are trying the get an item from an array that is beyond the the number of items in that array. (also accept some anser, people are more willing to anser if you accept answers)
Ex:
You do a plus 1 in the numberOfRowsInSection which, by example, return 11.
That means that there are 10 item in the itemsToDisplay array.
You are retrieving item from the itemsToDisplay in with MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row] but you cant retrieve item number 11 because it does not exists.
In the - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:
if (indexPath.row < [itemsToDisplay count]) {
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
// Set the cell here
} else {
//create a cell with the text "Load More"
}

Populating two grouped tableviews from the same PLIST

In my app, there is a portion that holds a static contacts directory. I would like the directory to appear indexed (alphabetically) and the detail view will need to be grouped also. (I am somewhat trying to replicate the look and feel of the Contacts app.) I have it working, just no index and a detail page that is just a view with a collection of buttons.
For some reason, I cannot get the a-ha moment when dealing with the table view.
Does anyone have any examples of how I can do this? Even better, what is the absolute best book to show how to work with UITableViews (especially when grouping them) using a PLIST as a source?
Apples documentation and other searches have gotten me some good information, but feels far from comprehensive enough to fully "get it".
First of if youre using pLists as the source then they wont stay in any form of order, well not the order there in the pList anyway. A way around this is to have an array within your pList which then has your elements. To us data from a plist you might want to do something like this if you had a plist populated with NSDictionarys:
NSDictionary* dict = [NSDictionary initwithContentsOfFile:#"ApList.plist"];
//Get all of the elements in the dictionary
NSArray *array = [dict allKeys};
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier;
MyIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[table dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
//Create a new dict based of the indexpath of the table cell, we need the array otherwise we wouldnt be able to get the key value for the dict.
NSDictionary* currentDict = [NSDictionary valueForKey:[array objectAtIndex:indexPath.row]];
//Then once you;ve got the dict create a string from a String held in that dict, in this case the key for the String is LABEL.
NSString *title = [currentDict valueForKey:#"LABEL"];
cell.textLabel.text = title;
return cell;
}
Hope this helps

date wise sorted uitableview

I am developing an iPhone application .
In the application I want to show the uitableview data sorted on the date field :
Suppose I have a Person object with fields name,birthdate,phone number etc.
Now I have array of Person and I am sorting that array on date field.
Now I am not understanding that how to handle these two methods ;
(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
i.e
How to count date wise objects and determine ?
Assuming you have one section and a sorted NSArray or NSMutableArray called _personArray:
- (NSInteger) numberOfSectionsInTableView:(UITableView *)_tableView {
return 1;
}
- (NSInteger) tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)_section {
return [_personArray count];
}
- (UITableViewCell *) tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)_indexPath {
Person *_person = [_personArray objectAtIndex:_indexPath.row];
NSString *_cellIdentifier = [NSString stringWithFormat: #"%d:%d", _indexPath.section, _indexPath.row];
UITableViewCell *_cell = [_tableView dequeueReusableCellWithIdentifier:_cellIdentifier];
if (_cell == nil) {
_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:_cellIdentifier] autorelease];
}
_cell.textLabel.text = _person.name;
_cell.detailTextLabel.text = _person.birthdate;
return _cell;
}
If you require multiple sections, split your array into multiple NSMutableArray instances, each containing Person instances associated with a specific section.
Then modify -numberOfSectionsInTableView: to return the number of sections (a count of the number of section arrays), as well as the other two methods to: 1) get element counts within a section array and; 2) to recall the right Person for a given indexPath.section and indexPath.row.

How to fill one UItableViewCell with odd and even entries from an NSMutableArray?

I have an uitableview with UITableViewCellSTyleValue1 so i have a textlabel and a detail text label.
The second thing i've got is a NSMutableArray. Now i want to fill the textLabels with the even items and the detailTextlabels with the odd entries.
So for example cell one(zero) gets entry 0 and 1 cell one gets 2 and 3 and so on.
I didn't get it working until now.
That must be pretty easy to do (if you have problems only with distributing array items between corresponding cell labels)...
As every cell "consumes" 2 array entry - number of cells equals [dataArray count]/2:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataArray count]/2;
}
Then in your cellForRowAtIndexPath implementation:
...
cell.textLabel.text = [dataArray objectAtIndex:2*indexPath.row];
cell.detailTextLabel.text = [dataArray objectAtIndex:2*indexPath.row + 1];
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// the usual cell creation/reuse stuff..
// obtain Label1, 2 by viewWithTag..
// index is your instance variable..
index = indexPath.row * 2;
Label1.text = [myArr objectAtIndex:index];
Label2.text = [myArr objectAtIndex:index + 1];
}