UITableViewController:cellForRowAtIndexPath继续循环第1节

时间:2013-08-15 01:10:45

标签: ios objective-c uitableview

我有5个部分。经过第0部分后,我不确定为什么cellForRowAtIndex会继续循环执行第1节。下面是代码和日志。感谢

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    numberOfSections = 0;

    if ([pastSevenDayArray count] > 0) numberOfSections++;
    if ([pastFourteenDaysArray count] > 0) numberOfSections++;
    if ([pastThirtyDaysArray count] > 0) numberOfSections++;
    if ([pastSixtyDaysArray count] > 0) numberOfSections++;
    if ([pastNinteyDaysArray count] > 0) numberOfSections++;
    if ([pastArray count] > 0) numberOfSections++;

    NSLog (@"numberofSections %i", numberOfSections);

    [self numberOfCells];

    return numberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 0)
    {
        return sectionZeroCellCount;
    }

    else if (section == 1)
    {
        return sectionOneCellCount;
    }
    else if (section == 2)
    {
        return sectionTwoCellCount;
    }
    else if (section == 3)
    {
        return sectionThreeCellCount;
    }
    else if (section == 4)
    {
        return sectionFourCellCount;
    }
    if (section == 5)
    {
        return sectionFiveCellCount;
    }
}

的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"IndexPath Section %i", [indexPath section]);


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (indexPath.section == 0)
    {
        NSLog(@"Entered Section 0");

    }
    else if (indexPath.section == 1)
    {
        NSLog(@"Entered Section 1");
    }

    else if (indexPath.section == 2)
    {
        NSLog(@"Entered Section 2");
    }

    else if (indexPath.section == 3)
    {
        NSLog(@"Entered Section 3");
    }

    else if (indexPath.section == 4)
    {
        NSLog(@"Entered Section 4");

    }

    else if (indexPath.section == 5)
    {
        NSLog(@"Entered Section 5");
    }

    NSString *entityName = [[object entity]name];
    //NSString *userName= [object valueForKey:@"recipientUserName"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];
    NSDate *date = [object valueForKey:@"lastmoddate"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"EEE, MMM d, YYYY  h:mm a"];
    NSString *dateString = [formatter stringFromDate:date];

    cell.detailTextLabel.text = dateString;
    object = NULL;

    return cell;
}

修改

numberOfCells

-(void)numberOfCells
{
    for (int section = 0; section<numberOfSections; section ++)
    {
        if (section == 0)
        {
            if ([pastSevenDayArray count] > 0)
            {
                SevenDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionZeroCellCount = [pastSevenDayArray count];
                sectionZeroHeader = sevenDaysSectionLabel;
            }
            else if ([pastFourteenDaysArray count] > 0)
            {
                fourteenDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionZeroCellCount = [pastFourteenDaysArray count];
                sectionZeroHeader = fourteenDaysSectionLabel;
            }
            else if ([pastThirtyDaysArray count] > 0)
            {
                thirtyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionZeroCellCount = [pastThirtyDaysArray count];
                sectionZeroHeader = thirtyDaysSectionLabel;
            }
            else if ([pastSixtyDaysArray count] > 0)
            {
                sixtyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionZeroCellCount = [pastSixtyDaysArray count];
                sectionZeroHeader = sixtyDaysSectionLabel;

            }
            else if ([pastNinteyDaysArray count] > 0)
            {
                ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionZeroCellCount = [pastNinteyDaysArray count];
                sectionZeroHeader = ninetyDaysSectionLabel;
            }
            else if ([pastArray count] > 0)
            {
                pastArraySectioned = [NSNumber numberWithBool:YES];
                sectionZeroCellCount = [pastArray count];
                sectionZeroHeader = pastSectionLabel;
            }
        }

        else if (section == 1)
        {    
            if (([pastFourteenDaysArray count] > 0) && (fourteenDaysArraySectioned != [NSNumber numberWithBool:YES]))

            {
                fourteenDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionOneCellCount = [pastFourteenDaysArray count];
                sectionOneHeader = fourteenDaysSectionLabel;
            }
            else if (([pastThirtyDaysArray count] > 0) && (thirtyDaysArraySectioned != [NSNumber numberWithBool:YES]))

            {
                thirtyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionOneCellCount = [pastThirtyDaysArray count];
                sectionOneHeader = thirtyDaysSectionLabel;
            }
            else if (([pastSixtyDaysArray count] > 0) && (sixtyDaysArraySectioned != [NSNumber numberWithBool:YES]))
            {
                sixtyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionOneCellCount = [pastSixtyDaysArray count];
                sectionOneHeader = sixtyDaysSectionLabel;
            }
            else if (([pastNinteyDaysArray count] > 0) && (ninetyDaysArraySectioned != [NSNumber numberWithBool:YES]))

            {
                ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionOneCellCount = [pastNinteyDaysArray count];
                sectionOneHeader = ninetyDaysSectionLabel;
            }
            else if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
            {
                pastArraySectioned = [NSNumber numberWithBool:YES];
                sectionOneCellCount = [pastArray count];
                sectionOneHeader = pastSectionLabel;
            }
        }
        else if (section ==2)
        {
            if (([pastThirtyDaysArray count] > 0) && (thirtyDaysArraySectioned != [NSNumber numberWithBool:YES]))

            {
                thirtyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionTwoCellCount = [pastThirtyDaysArray count];
                sectionTwoHeader = thirtyDaysSectionLabel;
            }
            else if (([pastSixtyDaysArray count] > 0) && (sixtyDaysArraySectioned != [NSNumber numberWithBool:YES]))
            {
                sixtyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionTwoCellCount = [pastSixtyDaysArray count];
                sectionTwoHeader = sixtyDaysSectionLabel;
            }
            else if (([pastNinteyDaysArray count] > 0) && (ninetyDaysArraySectioned != [NSNumber numberWithBool:YES]))

            {
                ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionTwoCellCount = [pastNinteyDaysArray count];
                sectionTwoHeader = ninetyDaysSectionLabel;
            }
            else if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
            {
                pastArraySectioned = [NSNumber numberWithBool:YES];
                sectionTwoCellCount = [pastArray count];
                sectionTwoHeader = pastSectionLabel;
            }
        }
        else if (section ==3)

        {
            NSLog(@"Entered Section %i", section);

            if (([pastSixtyDaysArray count] > 0) && (sixtyDaysArraySectioned != [NSNumber numberWithBool:YES]))            {
                sixtyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionThreeCellCount = [pastSixtyDaysArray count];
                sectionThreeHeader = sixtyDaysSectionLabel;
            }
            else if (([pastNinteyDaysArray count] > 0) && (ninetyDaysArraySectioned != [NSNumber numberWithBool:YES]))
            {
                ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionThreeCellCount = [pastNinteyDaysArray count];
                sectionThreeHeader = ninetyDaysSectionLabel;          
            }
            else if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
            {
                pastArraySectioned = [NSNumber numberWithBool:YES];
                sectionThreeCellCount = [pastArray count];
                sectionThreeHeader = pastSectionLabel;
            }
        }
        else if (section ==4)
        {
            NSLog(@"Entered Section %i", section);

            if (([pastNinteyDaysArray count] > 0) && (ninetyDaysArraySectioned != [NSNumber numberWithBool:YES]))
            {
                ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
                sectionFourCellCount = [pastNinteyDaysArray count];
                sectionFourHeader = ninetyDaysSectionLabel;
            }
            else if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
            {
                pastArraySectioned = [NSNumber numberWithBool:YES];
                sectionFourCellCount = [pastArray count];
                sectionFourHeader = pastSectionLabel;
            }
        }

        else if (section ==5)
        {
            NSLog(@"Entered Section %i", section);

            if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
            {
                pastArraySectioned = [NSNumber numberWithBool:YES];
                sectionFiveCellCount = [pastArray count];
                sectionFiveHeader = pastSectionLabel;
            }
        }

    }

}

numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 0)
    {
        return sectionZeroCellCount;
    }

    else if (section == 1)
    {
        return sectionOneCellCount;
    }
    else if (section == 2)
    {
        return sectionTwoCellCount;
    }
    else if (section == 3)
    {
        return sectionThreeCellCount;
    }
    else if (section == 4)
    {
        return sectionFourCellCount;
    }
    if (section == 5)
    {
        return sectionFiveCellCount;
    }
}

日志

2013-08-14 18:06:16.222 Time[5241:c07] numberofSections 5
2013-08-14 18:06:16.226 Time[5241:c07] IndexPath Section 0
2013-08-14 18:06:16.227 Time[5241:c07] Entered Section 0
2013-08-14 18:06:16.228 Time[5241:c07] IndexPath Section 1
2013-08-14 18:06:16.229 Time[5241:c07] Entered Section 1
2013-08-14 18:06:16.229 Time[5241:c07] IndexPath Section 1
2013-08-14 18:06:16.229 Time[5241:c07] Entered Section 1
2013-08-14 18:06:16.230 Time[5241:c07] IndexPath Section 1
2013-08-14 18:06:16.230 Time[5241:c07] Entered Section 1
2013-08-14 18:06:16.230 Time[5241:c07] IndexPath Section 1
2013-08-14 18:06:16.231 Time[5241:c07] Entered Section 1
2013-08-14 18:06:16.231 Time[5241:c07] IndexPath Section 1
2013-08-14 18:06:16.232 Time[5241:c07] Entered Section 1
2013-08-14 18:06:16.232 Time[5241:c07] IndexPath Section 1
2013-08-14 18:06:16.232 Time[5241:c07] Entered Section 1

3 个答案:

答案 0 :(得分:1)

每个部分每个可见行调用

tableView:cellForRowAtIndexPath:一次。此日志表示一个表视图变为可见,第0部分中有一行,第1部分中至少有六行。如果进一步滚动它,您可能会看到记录其他部分,因为它们的行变得可见。所以,这个日志是预期的行为。

答案 1 :(得分:0)

我相信如果你改变:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

为:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

它将起作用,除非您认为您还需要指定单元格的indexPath。

答案 2 :(得分:0)

我认为你的问题出在numberOfCells。

您正在与NSNumbers进行大量指针比较,如下所示:

pastArraySectioned != [NSNumber numberWithBool:YES]

当您比较那些NSNumber实例的POINTERS时,这不会起作用。如果你想测试pastArraySectioned是否为true,你只需要获得它的bool值:

[pastArraySectioned boolValue]