我试图在一个部分中获得前五张牌,在表格视图的下一部分中获得其他五张牌。实际上我已经完成了解析并且无法获得正确的单元格值。我得到的是组合值,例如:section = birthday
单元格值card1
,card2
再次card1
,card 2
new year section
,在其他部分,同样的事情正在重复。我应该怎么做才能分组,card1
,生日区卡2和card1
,card2
应该在新年部分。这是我的代码:
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
NSLog(@"section in rv");
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.books count];
NSLog(@".....appDelegate.books count");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cell for index path");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
NSLog(@"text for cell...");
cell.text = aBook.Subcatname;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
[appDelegate release];
}
答案 0 :(得分:1)
查看dks1725的响应,以设置每个部分的正确行数。
此外,更直接地回答您的问题,在每个表格行的tableView:cellForRowAtIndexPath:
中,您始终返回具有相同内容的单元格,无论该单元格位于哪个部分。这就是您获得每个部分中出现相同的内容('card1','card2'等)。
请注意,您在此方法中收到的indexPath
有两个组成部分:indexPath.row
和indexPath.section
。您忽略了其中的第二个,因此,对于每个部分,您总是为每一行返回相同的单元格。
您需要修改tableView:cellForRowAtIndexPath:
,以使其中的一部分看起来如下所示:
if (indexPath.section == 0) {
//Retrieve the content you want for each row in section 0
//It will look something like this, but will be different depending on where you stored your content
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
cell.text = birthdayBook.Subcatname;
}
if (indexPath.section == 1) {
//Retrieve the content you want for each row in section 1
//It will look something like this, but will be different depending on where you stored your content
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row + 5];
cell.text = newyearsBook.Subcatname;
}
...
根据您的意见编辑以上内容 鉴于您的模型,您也可以用更少的行来完成,如下所示:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2;} -(NSString *)tableView:(UITableView *)tblView titleForHeaderInSection:(NSInteger)section { NSString *sectionName = nil; switch(section) { case 0: sectionName = [NSString stringWithString:@"birthday"]; break; case 1: sectionName = [NSString stringWithString:@"new year"]; } return sectionName; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger count; if(section == 0) { count=5; } else if(section == 1) { count=5; } return count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cell for index path"); static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; } NSLog(@"text for cell..."); if(indexPath.section == 0) { Book *aBook = [appDelegate.books objectAtIndex:indexPath.row]; cell.text = aBook.Subcatname; } else if(indexPath.section == 1) { Book *aBook = [appDelegate.books objectAtIndex:indexPath.row+5*indexPath.section]; cell.text = aBook.Subcatname; } cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; [appDelegate release]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic -- create and push a new view controller if(bdvController == nil) bdvController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:[NSBundle mainBundle]]; NSLog(@"pushing to bdv controller"); if(indexPath.section == 0) { Book *aBook = [appDelegate.books objectAtIndex:indexPath.row]; bdvController.aBook = aBook; } else if(indexPath.section == 1) { Book *aBook = [appDelegate.books objectAtIndex:indexPath.row+5*indexPath.section]; bdvController.aBook = aBook; } NSLog(@"push to view descrip, id, pic url"); //bdvController.aBook = aBook; NSLog(@"loop"); [self.navigationController pushViewController:bdvController animated:YES]; }
答案 1 :(得分:0)
您必须为每个部分设置行数,如下所示。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(indexPath.section==0)
//return no of rows for section 0
else
//return no of rows for section 1
}