I create the following data on viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
food = [[NSMutableArray alloc] initWithObjects: @"Harvey's",@"McCormicks",@"Subway",nil];
gifts = [[NSMutableArray alloc] initWithObjects:@"The gift Shop",@"Lilo Gifts",@"Prezzies for All", nil];
services = [[NSMutableArray alloc] initWithObjects:@"Hair Snippers",@"Laundro-mat",@"Accountants",@"Shoe fitters", nil];
hotel = [[NSMutableArray alloc] initWithObjects:@"Hotel Paradiso",@"Dick & Doms B&B",@"Super Hotel",@"Castle B&B", nil];
pubs = [[NSMutableArray alloc] initWithObjects:@"The Snuggly Duckling",@"Beer's Arms",@"Merlins Beard", @"Cheers", nil];
}
Here are my data source methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 5;
}
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return NSLocalizedString(@"Food & Restaraunts", nil);
case 1:
return NSLocalizedString(@"Shopping/ Gifts", nil);
case 2:
return NSLocalizedString(@"Services", nil);
case 3:
return NSLocalizedString(@"Hotels / B&B's", nil);
case 4:
return NSLocalizedString(@"Pubs",nil);
default:
return nil;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return 3;
case 1:
return 3;
case 2:
return 4;
case 3:
return 4;
case 4:
return 4;
//default:
// return 1;
}
}
// 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];
//mine
switch (indexPath.section) {
case 0:
[[ cell textLabel]
setText:[food objectAtIndex:indexPath.row]];
break;
case 1:
[[ cell textLabel]
setText:[gifts objectAtIndex:indexPath.row]];
break;
case 2:
[[ cell textLabel]
setText:[services objectAtIndex:indexPath.row]];
break;
case 3:
[[ cell textLabel]
setText:[hotel objectAtIndex:indexPath.row]];
break;
case 4:
[[ cell textLabel]
setText:[pubs objectAtIndex:indexPath.row]];
break;
}
return cell;
}
}
What its actually doing is displaying the first 10 lines correctly then repeating the list again, if I add more lines to the first couple of sections it repeats earlier up the table.. I’ve cleared the cache etc but nothing works.
That’s because you set contents of your reusable cells only once.
I am not good at post formatting. You should set content of your cell after taking it from “reuse stack”.