UITableview中的节扩展 - 再次推到视图时,展开的部分会消失

时间:2013-12-23 07:54:06

标签: iphone uitableview

尝试扩展UITableview中的部分。一切正常,但如果展开的部分未关闭且视图弹出,然后再次推送到该视图,那么上次展开的部分会消失,如果tableview中的任何部分选择了应用程序崩溃。我通过断点检查了问题是下次从section1开始而不是从section0开始。为什么会这样,我没有得到。以下是我正在使用的代码。

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.arrList count];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
    if(indexSectionOne)
    {
        return [self.arrProductsList count];
    }
    else
    {
        return 1;
    }
}

if(section == 1)
{
    if(indexSectionTwo)
    {
        return [self.arrServicesList count];
    }
    else
    {
        return 1;
    }
}

else
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"CellIdentifier";

AboutCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if(cell==nil)
{
    cell = [[AboutCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
/*
if((indexPath.section == 0)||(indexPath.section == 1))
{
    if(indexPath.row == 0)
    {
        cell.imgDwnView.image = [UIImage imageNamed:@"down_arrow_about_iPhone.png"];
    }
}
*/
NSLog(@"row:%d section:%d", indexPath.row, indexPath.section);
if(indexPath.section == 0)
 {
     if(indexPath.row == 0)
     {
         NSLog(@"row:%d section:%d", indexPath.row, indexPath.section);
         cell.imgDwnView.image = [UIImage imageNamed:@"down_arrow_about_iPhone.png"];
     }
     //if(indexPath.row != 0)
     else
     {
         NSLog(@"row:%d section:%d", indexPath.row, indexPath.section);
         cell.imgView.image = [UIImage imageNamed:[self.arrImages objectAtIndex:0]];
         cell.lblView.text = [[self.arrProductsList objectAtIndex:indexPath.row-1] valueForKey:@"ProductName"];
         cell.txtView.text = [[self.arrProductsList objectAtIndex:indexPath.row-1] valueForKey:@"ProductDescription"];
     }
 }
if(indexPath.section == 1)
{
    if(indexPath.row == 0)
    {
        NSLog(@"row:%d section:%d", indexPath.row, indexPath.section);
        cell.imgDwnView.image = [UIImage imageNamed:@"down_arrow_about_iPhone.png"];
    }
    //if(indexPath.row != 0)
    else
    {
        NSLog(@"row:%d section:%d", indexPath.row, indexPath.section);
        cell.imgView.image = [UIImage imageNamed:[self.arrImages objectAtIndex:1]];
        cell.lblView.text = [[self.arrServicesList objectAtIndex:indexPath.row-1] valueForKey:@"LinkTitle"];
        cell.txtView.text = [[self.arrServicesList objectAtIndex:indexPath.row-1] valueForKey:@"ProductDescription"];
    }
}

if(indexPath.row == 0)
{
    NSLog(@"row:%d section:%d", indexPath.row, indexPath.section);
    cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_background_about_iPhone.png"]];
    cell.lblView.text = [self.arrList objectAtIndex:indexPath.section];
    cell.imgView.image = [UIImage imageNamed:[self.arrImages objectAtIndex:indexPath.section]];
    cell.lblView.font = [UIFont fontWithName:@"NeoSansCyr-Regular" size:15.0f];
    cell.lblView.textColor = [UIColor colorWithRed:144.0/255.0 green:8.0/255.0 blue:8.0/255.0 alpha:1.0];
}
else
{
    NSLog(@"row:%d section:%d", indexPath.row, indexPath.section);
    cell.lblView.frame = CGRectMake(60, 15, 150, 25);
    cell.imgView.frame = CGRectMake(13, 15, 30, 30);
    cell.txtView.frame = CGRectMake(60, 42, 180, 50);
    cell.imgDwnView.frame = CGRectMake(270, 18, 19, 10);
    cell.imgDwnView.image = [UIImage imageNamed:@"down_arrow_about_iPhone.png"];
    //cell.lblView.text = @"Labels";
   // cell.imgView.image = [UIImage imageNamed:@"Icon-small.jpg"];
   // cell.txtView.text = @"Description";
    cell.contentView.backgroundColor = [UIColor whiteColor];
}
return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
    indexSectionOne = !indexSectionOne;
    if(indexSectionOne)
    {
    }
    else
    {
    }
    [self.tblAboutView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}

if(indexPath.section == 1)
{
    indexSectionTwo = !indexSectionTwo;
    if(indexSectionTwo)
    {
    }
    else
    {
    }
    [self.tblAboutView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}


if(indexPath.section == 2)
{
    MapViewController *mapView = [[MapViewController alloc]initWithNibName:@"MapViewController" bundle:nil];
    mapView.strTitle = @"Branches/ATM";
    [self.navigationController pushViewController:mapView animated:YES];
}


if(indexPath.section == 3)
{
    MapViewController *mapView = [[MapViewController alloc]initWithNibName:@"MapViewController" bundle:nil];
    mapView.strTitle = @"Sadara Centre";
    [self.navigationController pushViewController:mapView animated:YES];
}


if(indexPath.section == 4)
{
    NSString *strRoadAsstNo = [dictData valueForKey:@"RoadSideAssistanceNo"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strRoadAsstNo]];
}


if(indexPath.section == 5)
{
    NSString *strContactNo = [dictData valueForKey:@"ContactCenterNo"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strContactNo]];
}

}

Update:
 Error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (33) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

提前致谢。请在上面指导我做错的地方。

0 个答案:

没有答案