1

我有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;
}

编辑

细胞数

-(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
4

3 回答 3

1

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

于 2013-08-15T15:24:31.333 回答
0

我相信如果你改变:

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

到:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

于 2013-08-15T01:20:33.710 回答
0

我认为您的问题出在 numberOfCells 中。

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

pastArraySectioned != [NSNumber numberWithBool:YES]

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

[pastArraySectioned boolValue]
于 2013-08-15T14:18:02.783 回答