Hello Guys Im starting with the SDK and I am trying to set an app using a tableview to display the info, Im going to say thanks in advance, because I know some of you experts can help me with my problem.
I created a window base application and Im using a tab bar with a tableview that has a navigation bar to go back and forward in it, and it works fine "apparently", but I am having The problem that Whenever you tap the first row of each (1,2,3...) section it calls the first row of the first section on and on, and I have done a lot of research: tutorials, books, forums, but cant seem to find a way to make it work. I found a lot of good information although that helped me a lot, but wouldn't fix my problem. I have been fitting with it for days and days.
So having tried all the resources that I was able to find I cry to you guys for your help.
here is the code I attempt to use, I think it would be of some help to some of you guys ether.
// Im using This in the MyTableViewController.h {
#import <UIKit/UIKit.h>
#import "firstCustomCell.h"
@interface MyTableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITableView *theTableView;
NSMutableArray *firstArray;
NSMutableArray *secondArray;
NSMutableArray *thirdArray;
NSMutableArray *listOfItems;
}
@property (nonatomic, retain) NSMutableArray *firstArray;
@property (nonatomic, retain) NSMutableArray *secondArray;
@property (nonatomic, retain) NSMutableArray *thirdArray;
@property (nonatomic, retain) NSMutableArray *listOfItems;
@end
}
// Then in the implementation Im using This: tableViewController.m
#import "MyTableViewController.h"
#import "One.h"
#import "Two.h"
#import "Three.h"
#import "Four.h"
#import "Five.h"
#import "Six.h"
#import "Seven.h"
#import "Eight.h"
#import "Nine.h"
#import "Ten.h"
#import "Eleven.h"
#import "Twelve.h"
@implementation MyTableViewController
@synthesize firstArray;
@synthesize secondArray;
@synthesize thirdArray;
@synthesize listOfItems;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
listOfItems = [[NSMutableArray alloc]init];
firstArray = [[NSMutableArray alloc]init];
[firstArray addObject:@"Week One"];
[firstArray addObject:@"Week Two"];
[firstArray addObject:@"Week Three"];
NSDictionary *firstDic = [NSDictionary dictionaryWithObject:firstArray forKey:@"CompoNente"];
secondArray = [[NSMutableArray alloc]init];
[secondArray addObject:@"Week Four"];
[secondArray addObject:@"Week Five"];
[secondArray addObject:@"Week Six"];
[secondArray addObject:@"Week Seven"];
NSDictionary *secondDic = [NSDictionary dictionaryWithObject:secondArray forKey:@"CompoNente"];
thirdArray = [[NSMutableArray alloc]init];
[thirdArray addObject:@"Week Eight"];
[thirdArray addObject:@"Week Nine"];
[thirdArray addObject:@"Week Ten"];
[thirdArray addObject:@"Week Eleven"];
[thirdArray addObject:@"Week Twelve"];
NSDictionary *thirdDic = [NSDictionary dictionaryWithObject:thirdArray forKey:@"CompoNente"];
[listOfItems addObject:firstDic];
[listOfItems addObject:secondDic];
[listOfItems addObject:thirdDic];
self.title = NSLocalizedString(@"TableView", @"My favorite TableView");
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [listOfItems count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSDictionary *dictionary = [listOfItems objectAtIndex:section];
NSArray *array = [dictionary objectForKey:@"CompoNente"];
return [array count];
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ScustomCell";
firstCustomCell *cell = (firstCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(@"Cell created");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"firstCustomCell" owner:nil options:nil];
for(id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[firstCustomCell class]]) {
cell = (firstCustomCell *)currentObject;
}
}
}
switch (indexPath.row) {
case 0:
(indexPath.section == 0, indexPath.row == 0);
[[cell currentNameLabel] setText:@"Week One"];
[[cell imageView] setImage:[UIImage imageNamed:@"1.png"]];
break;
case 1:
(indexPath.section == 0, indexPath.row == 1);
[[cell currentNameLabel] setText:@"Week Two"];
[[cell imageView] setImage:[UIImage imageNamed:@"2.png"]];
break;
case 2:
(indexPath.section == 0, indexPath.row %12 == 2);
[[cell currentNameLabel] setText:@"Week Three"];
[[cell imageView] setImage:[UIImage imageNamed:@"3.png"]];
break;
case 3:
(indexPath.section == 1, indexPath.row %12 == 0);
[[cell currentNameLabel] setText:@"Week Four"];
[[cell imageView] setImage:[UIImage imageNamed:@"4.png"]];
break;
case 4:
(indexPath.section == 1, indexPath.row %12 == 1);
[[cell currentNameLabel] setText:@"Week Five"];
[[cell imageView] setImage:[UIImage imageNamed:@"5.png"]];
break;
case 5:
(indexPath.section == 1, indexPath.row %12 == 2);
[[cell currentNameLabel] setText:@"Week Six"];
[[cell imageView] setImage:[UIImage imageNamed:@"6.png"]];
break;
case 6:
(indexPath.section == 1, indexPath.row %12 == 3);
[[cell currentNameLabel] setText:@"Week Seven"];
[[cell imageView] setImage:[UIImage imageNamed:@"7.png"]];
break;
case 7:
(indexPath.section == 2, indexPath.row %12 == 0);
[[cell currentNameLabel] setText:@"Week Eight"];
[[cell imageView] setImage:[UIImage imageNamed:@"8.png"]];
break;
case 8:
(indexPath.section == 2, indexPath.row %12 == 1);
[[cell currentNameLabel] setText:@"Week Nine"];
[[cell imageView] setImage:[UIImage imageNamed:@"9.png"]];
break;
case 9:
(indexPath.section == 2, indexPath.row %12 == 2);
[[cell currentNameLabel] setText:@"Week Ten"];
[[cell imageView] setImage:[UIImage imageNamed:@"10.png"]];
break;
case 10:
(indexPath.section == 2, indexPath.row %12 == 3);
[[cell currentNameLabel] setText:@"Week Eleven"];
[[cell imageView] setImage:[UIImage imageNamed:@"11.png"]];
break;
case 11:
(indexPath.section == 2, indexPath.row %12 == 4);
[[cell currentNameLabel] setText:@"Week Twelve"];
[[cell imageView] setImage:[UIImage imageNamed:@"12.png"]];
break;
}
// Configure the cell...
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"CompoNente"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.text = cellValue; //also receive a warning that setText is deprecated.
return cell;
}
//Then the delegate
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[firstArray objectAtIndex:indexPath.row] isEqual: @"Week One"]) {
One *one = [[One alloc]initWithNibName:@"One" bundle:nil];
[self.navigationController pushViewController:one animated:YES];
[one release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Two"]) {
Two *two = [[Two alloc]initWithNibName:@"Two" bundle:nil];
[self.navigationController pushViewController:two animated:YES];
[two release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Three"]) {
Three *three = [[Three alloc]initWithNibName:@"Three" bundle:nil];
[self.navigationController pushViewController:three animated:YES];
[three release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Four"]) {
Four *four = [[Four alloc]initWithNibName:@"Four" bundle:nil];
[self.navigationController pushViewController:four animated:YES];
[four release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Five"]) {
Five *five = [[Five alloc]initWithNibName:@"Five" bundle:nil];
[self.navigationController pushViewController:five animated:YES];
[five release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Six"]) {
Six *six = [[Six alloc]initWithNibName:@"Six" bundle:nil];
[self.navigationController pushViewController:six animated:YES];
[six release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Seven"]) {
Seven *seven = [[Seven alloc]initWithNibName:@"Seven" bundle:nil];
[self.navigationController pushViewController:seven animated:YES];
[seven release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Eight"]) {
Eight *eight = [[Eight alloc]initWithNibName:@"Eight" bundle:nil];
[self.navigationController pushViewController:eight animated:YES];
[eight release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Nine"]) {
Nine *nine = [[Nine alloc]initWithNibName:@"Nine" bundle:nil];
[self.navigationController pushViewController:nine animated:YES];
[nine release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Ten"]) {
Ten *ten = [[Ten alloc]initWithNibName:@"Ten" bundle:nil];
[self.navigationController pushViewController:ten animated:YES];
[ten release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Eleven"]) {
Eleven *eleven = [[Eleven alloc]initWithNibName:@"Eleven" bundle:nil];
[self.navigationController pushViewController:eleven animated:YES];
[eleven release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Twelve"]) {
Twelve *twelve = [[Twelve alloc]initWithNibName:@"Twelve" bundle:nil];
[self.navigationController pushViewController:twelve animated:YES];
[twelve release];
}
}
//adding the headers for sections
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0)
return @"FIRST SECTION";
else if (section == 1)
return @"SECOND SECTION";
else if (section == 2)
return @"THIRD SECTION";
} // also receive a warning "Control reaches end of non-void function"
//Release or clean memory And finishing TableView
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[listOfItems release];
[thirdArray release];
[secondArray release];
[firstArray release];
[super dealloc];
}
@end
Any help will be appreciated I am beyond desperation and if I can help you guys somehow conceder it done.