Text not being displayed in Grouped UITableView - iphone

The text for each cell in a UITableView is not being displayed, and I cannot for the life of me figure out why. I have 3 sections in the table view, and the sections are shown correctly, as is the number of rows for each section. However, the text inside each row for all the sections is empty. My code is:
- (void)viewDidLoad {
[super viewDidLoad];
listofsections = [[NSMutableArray alloc] init];
NSArray *shuffle = [NSArray arrayWithObjects:
#"Display words in alphabetical order", #"Display words in shuffled order", nil];
NSDictionary *shuffleDict = [NSDictionary dictionaryWithObject:shuffle forKey:#"Object Key"];
NSArray *wordFilter = [NSArray arrayWithObjects:
#"Show all words", #"Show words not yet appeared", #"Show words marked correct", #"Show words marked incorrect", #"Show words marked as favorite", #"Show words saved for later", nil];
NSDictionary *wordFilterDict = [NSDictionary dictionaryWithObject:wordFilter forKey:#"Object Key"];
NSArray *wordDisplay = [NSArray arrayWithObjects:
#"Display words first", #"Display definition first", nil];
NSDictionary *wordDisplayDict = [NSDictionary dictionaryWithObject:wordDisplay forKey:#"Object Key"];
[listofsections addObject:shuffleDict];
[listofsections addObject:wordFilterDict];
[listofsections addObject:wordDisplayDict];
// Do any additional setup after loading the view from its nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [listofsections count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Number of rows it should expect should be based on the section
NSDictionary *dictionary = [listofsections objectAtIndex:section];
NSArray *array = [dictionary objectForKey:#"Object Key"];
return [array count];
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
//return UITableViewCellAccessoryDetailDisclosureButton;
return UITableViewCellAccessoryNone;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0) {
return #"Word Display Order";
}
if (section == 1) {
return #"Word Filter";
}
else
return #"Display Selection";
}
// 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];
}
// Set up the cell...
//First get the dictionary object
NSDictionary *dictionary = [listofsections objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Countries"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

The dictionaries you have created dont have the key #"Countries" wich you have used in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Try changing your init method for the cells with :
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
instead of the deprecated initWithFrame.
And also make sure that the string you're trying to set is not nil.

Related

Data from Plist is Incorrectly Ordered in UITableView

I have data that is stored in a plist but when I pull it to my UITableView it gets reordered for some reason. Here are my table view data source methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.lunch_Dinner count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[self.lunch_Dinner allKeys] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *typeOfEntree = [self tableView:tableView titleForHeaderInSection:section];
return [[self.lunch_Dinner valueForKey:typeOfEntree] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EntreeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *typeOfEntree = [self tableView:tableView titleForHeaderInSection:indexPath.section];
NSString *entree = [[self.lunch_Dinner valueForKey:typeOfEntree] objectAtIndex:indexPath.row];
cell.textLabel.text = entree;
return cell;
}
Here is the order of the plist:
Appetizers
Soups
Pastas
Pizzas
Specials
And this is the resulting order in the UITableView after being compiled:
Pizzas
Soups
Appetizers
Pastas
Specials
Any help would be great!
Thanks in advance.
If you want to store table in plist it is better to consider the following structure:
NSArray *sections = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObject:
[NSArray arrayWithObjects:row_1, row_2, ... , nil]
forKey:#"section name 1"],
[NSDictionary dictionaryWithObject:
[NSArray arrayWithObjects:row_1, row_2, ... , nil]
forKey:#"section name 2"],
...
[NSDictionary dictionaryWithObject:
[NSArray arrayWithObjects:row_1, row_2, ... , nil]
forKey:#"section name N"],
nil];
This code representation is easy to reproduce as plist. Create it as in example below
This structure can be easily used as UITableView datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.datasource count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSDictionary *dict = [self.datasource objectAtIndex:section];
return [dict.allKeys lastObject];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *dict = [self.datasource objectAtIndex:section];
return [dict.allValues.lastObject count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [self.datasource objectAtIndex:indexPath.section];
id cellPresenter = [dict.allValues.lastObject objectAtIndex:indexPath.row];
...
}
It looks like lunch_Dinner is a NSDictionary. NSDictionarys are unordered, so you are not guaranteed a specific output order from them.
It is a little hard to see exactly what value you're getting from having a key/value pair here, but one option would be to keep a separate NSArray with the correct ordering, and use that to correctly populate things.

UITableView: use object from dictionary as title on detail view

I have the following code to make a table from string turned into a dictionary:
- (void)viewDidLoad {
[super viewDidLoad];
testArray = [[NSArray alloc] init];
NSString *testString = #"Sam|26,Hannah|22,Adam|30,Carlie|32";
testArray = [testString componentsSeparatedByString:#","];
dict = [NSMutableDictionary dictionary];
for (NSString *s in testArray) {
testArray2 = [s componentsSeparatedByString:#"|"];
[dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]];
}
}
- (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];
}
// Configure the cell.
if (testArray.count >indexPath.row) {
cell.textLabel.text = [[dict allKeys] objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [dict objectForKey:cell.textLabel.text];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
What I would like is for the selected row title to be set as the title on my detail view.
I tried with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.detailController.title = [[dict allKeys] objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:self.detailController animated:YES];
}
But I get an "EXC_BAD_ACCESS" error.
It works fine if I use #"1" as title, it's just something with my dictionary call that's wrong, I assume.
Make dict a retained dictionary instead of an autoreleased one.
I.E. declare it maybe like this:
dict = [[NSMutableDictionary alloc] initWithCapacity: [testArray count]];
in your viewDidLoad method. Make sure to release it when viewDidUnload is called.
Also, make sure of the number of keys in your dict before calling:
self.detailController.title = [[dict allKeys] objectAtIndex:[indexPath row]];
So, I would do:
if(dict && ([[dict allKeys] count] > [indexPath row])
{
self.detailController.title =
[[dict allKeys] objectAtIndex:[indexPath row]];
} else {
self.detailController.title = #"Here's a problem";
}
Did you implement these UITableView delegate methods ? All these are needed. Also can you post more detailed StackTrace.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [resultSet count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 280.0;
}

How To Structure Array Into UITableView?

I have an array with the following format:
[NSArray arrayWithObjects:
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
nil];
I want to structure this data to load into my tableView.
Each row of the tableview should correspond to each row of the array, then the title of each cell should correspond to the subarray objectAtIndex 2 for the name.
Suppose your array is named myData:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [myData count];
}
- (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];
}
// Configure the cell...
NSArray *obj = [myData objectAtIndex:indexPath.row];
cell.textLabel.text = (NSString*)[obj objectAtIndex:1]; //note: 0=>number; 1=>name,..
return cell;
}
In favor of reusability, I would suggest to replace the subarrays with NSDictionaries so that you can get e.g. the name by calling [dict objectForKey:#"name"].

Not able to access array with ids in another view controller in iphone

these is some code from my project..
for (index=0; index<[feed count]; index++) {
//NSLog(#"........%#",[feed objectAtIndex:2]);
NSString* forName=[feed objectAtIndex:index];
NSString *nameString = [forName valueForKey:#"name"];
NSLog(#"--------%#",nameString);
[nameArray addObject:nameString];
NSString *idString = [forName valueForKey:#"id"];
NSLog(#"--------%#",idString);
[idArray addObject:idString];
}
MyFriends *detailViewController = [[MyFriends alloc] initWithNibName:#"MyFriends" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailViewController.fbNames=nameArray;
detailViewController.fbIds=idArray;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
My problem is in another controller when I am accessing array with names which is fbNames in another class it is showing me proper names,But when I am accessing fbIds which is the array with Ids ...they are showing me null..
how to correct this?
here...in this class ...fbNames are working fbIds are not..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [fbNames 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];
}
// Configure the cell...
cell.textLabel.text=[fbNames objectAtIndex:indexPath.row];
NSLog(#"%#",cell.textLabel.text);
return cell;
}
NSString* forName=[feed objectAtIndex:index];
NSString *nameString = [forName valueForKey:#"name"];
I don't think NSString has valueForKey:

I can't fill the cells of my UITableView with a NSArray (Normally it always works, but not in the case)

I have a UITableView with Cells that are filled with the Objects of an NSArray. I´m using the MGTwitterEngine to get Tweets from users. I want to show this tweets in the TableView. Here is the method that is called when the MGTwitterEngine has received all tweets ("namen" and "nachricht" are NSArrays):
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
{ NSLog(#"Got statuses for %#:\r%#", connectionIdentifier, statuses);
NSDictionary *userDict = [statuses valueForKey:#"user"];
namen = [[NSArray alloc] initWithObjects: [userDict valueForKey:#"name"], nil];
nachricht = [statuses valueForKey:#"text"];
NSLog(#"%#", namen);
NSLog(#"%#", nachricht);
[table reloadData];
}
Then I did this to fill the cells with the names of the Twitter-users:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [nachricht count];
}
- (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];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
return cell;
}
Unfortunately, the app crashes whenever the table is reloaded: [table reloadData];. I know that the NSArray got all the right values, because NSLog shows me that. I've tried also to create a other NSArray, like that:
arryData = [[NSArray alloc] initWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
And I written this into the cellforRowAtIndexPath-methode and and everything worked well. Why does it not work with the "namen"-NSArray?
The NSArray is probably being released - have you examined what's at its memory address?