How To Structure Array Into UITableView? - iphone

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"].

Related

Data not showing in iphone tableView

I am getting data from json. It is working OK but the problem is that data is not showing in tableView.
I have checked the values that I assign to cell using NSLog but cell is not displaying any data.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = [surveyList count];
NSLog(#"These are rows %d",count);
return [surveyList count];
}
- (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];
}
ObjectData *data = [surveyList objectAtIndex:indexPath.row];
NSString *testingClient = data.survey_title;
NSLog(testingClient);
NSString *cellText = testingClient;
return cell;
}
Are you not missing this?
[[cell textLabel] setText:testingClient];
You aren't assigning any text to the cell without it.
You should assign that text to label of cell.
cell.textLabel.text = testingClient;

Correct Approach to Populating a Table View Using An Array?

I'm currently trying to implement a Table View on my iPhone app that grabs an array from one table view/class and using this array to populate a table in another seperate view. Here is the method I use that adds an exercise to an array if it is clicked/tapped:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
NSUInteger *index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
[array insertObject:str atIndex:index];
self.workout = array;
[array release];
}
Once the save button is pressed, this array will be stored in an array of workouts (arrays) that I want to be populated in another view. Am I taking the correct approach this?
Here is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SectionsTableIdentifier ] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
you probably do not want to re-initialize self.workout with a new array each time.
-(void) viewDidLoad
{
self.workout = [[NSMutableArray alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
[self.workout insertObject:str atIndex:0];
}

How to implement sections in table view containing SQLite3 database?

I have an application which display large amount of values using UITableView and imported sqlite3 database. Now i face problem that i need to make sections for elements. It must be similar for default application "contacts" in iPhone, but i don't want for that section to sort elements only for first letter. Database contains data about food, so sections should be like "Bread", "Meat", "Fish", etc.
Please help, any advice will be appreciated!
Make a NSDictionary whose key is title to be displayed and value is NSArray which consist of all the objects that contain. Now do the following :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[dict allkeys]count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection(NSInteger)section
{
//Number of rows it should expect should be based on the section
NSArray *array = [dict objectForKey:[[dict allKeys] objectAtIndex:section]];
return [array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[dict allKeys] objectAtIndex:section];
}
- (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...
cell.text = [[dict objectForkey:[[dict allKeys] objectAtIndex:indexpath.section]] objectAtIndex:indexPath.row];
return cell;
}

Text not being displayed in Grouped UITableView

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.

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: