开发者

Section receiving cell values of other section

I am trying to get the first five cards in one section and other five in next section of table view . Actually I have done parsing and unable to get proper cell values. The thing I am getting is combined values for example : in section = birthday cell values are card1, card2 and again card1, card 2 of new year section, and in other section same thing is repeating. What should I do to make grouped i.e card1, card 2 in birthday section and card1, card2 should be in new year section. Here is my code:

 (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
    NSLog(@"section in rv");
}

 (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSectio开发者_Go百科n:(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];
  }


Take a look at dks1725's response to set up the correct number of rows per section.

Also, and more directly in response to your question, in tableView:cellForRowAtIndexPath: for each table row you are always returning a cell with the same content, regardless of what section the cell is in. That's why you are getting the same content ('card1', 'card2' and so on) appearing in each section.

Keep in mind that the indexPath you are receiving in this method has two components: indexPath.rowand indexPath.section. You are ignoring the second of these, and so, for every section, you always return the same cell for a each row.

You will need to modify tableView:cellForRowAtIndexPath: so that a piece of it will look something like the following:

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

The above is edited as per your comments.
Given your model, you could also do it in fewer lines, as follows:

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



You have to set number of rows for each section something like below..

- (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
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜