开发者

Iphone Grouped tableview with nib file by row, my rows restart in every section and it wont show the next section rows

I'm starting with the SDK and I am trying to set an app using a tableview to display the info.

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 for help.

Here is the code I attempt to use, I think it would be of some help to some of you 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 viewDidLo开发者_Python百科ad];

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


Yeah I always have this problem - I'm not sure what it is but I think its something to do with the fact that == 0 is also == NO or == false, but I'm not sure.

What usually works for me is treating it like an integer, so:

if([indexPath section] < 1){
  // section 0
}
else if([indexPath section] > 0 && [indexPath section] < 2){
  // section 1
}
else{
  // etc...
}

or you could convert it to an NSString

NSString *section = [NSString stringWithFormat:@"%i",[indexPath section]];

and then do a string comparison:

if([section isEqualToString:@"0"]){
  // first section
}
else if([section isEqualToString:@"1"]){
  // next section
}
else {
  // etc...
}


in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath you are just ignoring indexpath.section. There is no call that takes the section into account.


are your section headers working or not? For me this looks okay.
Add an return nil; add the end of this method to get rid of this warning. You get this warning because you are not returning anything if section is bigger than 2.


 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]; 
  }

ehm, did you really create 10 different classes with 10 different nib-files for 10 different weeks? I doubt that this is really necessary. This will get you into a lot of copy&paste if your customers want support for 52 weeks in the next version ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜