我在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];
}
以下是我的数据源方法:
- (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;
}
}
它实际上正在做的是正确地显示前10行然后再次重复列表,如果我向前几个部分添加更多行,它在表格的前面重复...我已经清除了缓存等但没有任何作用。
答案 0 :(得分:1)
这是因为您只设置了一次可重复使用的单元格的内容。
- (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;}
我不擅长发布格式。从“重用堆栈”中取出后,您应该设置单元格的内容。