iOS版:的UITableViewController - 的cellForRowAtIndexP

2019-10-18 12:39发布

我知道苹果的文档不指定的cellForRowAtIndexPath就会按照顺序,即首先,它会去第0,索引0; 然后第0,索引2; 然后第1,指数0 ....等等。 我认为它跳动。

什么是更好的方式到以下完成? 目前,我不断收到一个错误

NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]'

B / C我想的cellForRowAtIndexPath跳跃周围和标记显示错误*sectioned = NULL ,所以条件被跳过,它进入一个空数组或转至错误的条件超出指标数。

下面是代码:

... declared and synthesized 
NSManaged *object;
@property (strong, nonatomic) NSNumber *sevenDaysSectionFilled;
@property (strong, nonatomic) NSNumber *fourteenDaysSectionFilled;
@property (strong, nonatomic) NSNumber *thirtyDaysSectionFilled;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    object = NULL;
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (indexPath.section == 0)
    {
        if ((SevenDaysArraySectioned == [NSNumber numberWithBool:YES]) && (sevenDaysSectionFilled != [NSNumber numberWithBool:YES]))
        {
            object = [pastSevenDayArray objectAtIndex:indexPath.row];
            sevenDaysSectionFilled = [NSNumber numberWithBool:YES];
        }

        else if ((fourteenDaysArraySectioned == [NSNumber numberWithBool:YES]) && (fourteenDaysSectionFilled != [NSNumber numberWithBool:YES]))
        {
            object = [pastFourteenDaysArray objectAtIndex:indexPath.row];
            fourteenDaysSectionFilled = [NSNumber numberWithBool:YES];
        }

        else if ((thirtyDaysArraySectioned == [NSNumber numberWithBool:YES]) && (thirtyDaysSectionFilled != [NSNumber numberWithBool:YES]))
        {
            object = [pastThirtyDaysArray objectAtIndex:indexPath.row];
            thirtyDaysSectionFilled = [NSNumber numberWithBool:YES];
        }

    else if (indexPath.section == 1)
    {
        if ((fourteenDaysArraySectioned == [NSNumber numberWithBool:YES]) && (fourteenDaysSectionFilled != [NSNumber numberWithBool:YES]))
        {
            object = [pastFourteenDaysArray objectAtIndex:indexPath.row];
            fourteenDaysSectionFilled = [NSNumber numberWithBool:YES];
        }

        else if ((thirtyDaysArraySectioned == [NSNumber numberWithBool:YES]) && (thirtyDaysSectionFilled != [NSNumber numberWithBool:YES]))
        {
            object = [pastThirtyDaysArray objectAtIndex:indexPath.row];
            thirtyDaysSectionFilled = [NSNumber numberWithBool:YES];
        }

    else if (indexPath.section == 2)
    {
        if ((thirtyDaysArraySectioned == [NSNumber numberWithBool:YES]) && (thirtyDaysSectionFilled != [NSNumber numberWithBool:YES]))
        {
             object = [pastThirtyDaysArray objectAtIndex:indexPath.row];
            thirtyDaysSectionFilled = [NSNumber numberWithBool:YES];
        }
     }

NSString *entityName = [[object entity]name];
cell.textLabel.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];

  object = NULL;
  return cell;
}

请让我知道是否有更好的方法来做到这一点。 谢谢。

* 编辑

这是一个有点复杂,但在这里它是:

- (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++;

    [self numberOfCells];


    return numberOfSections;
}

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

编辑2

标题:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 150)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 25)];
    label.backgroundColor = [UIColor clearColor];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    label.textColor = [UIColor whiteColor];

    if (section == 0)
    {
        label.text = sectionZeroHeader;
    }
    else if (section == 1)
    {
        label.text = sectionOneHeader;
    }

    else if (section == 2)
    {
        label.text = sectionTwoHeader;
    }
    else if (section == 3)
    {
        label.text = sectionThreeHeader;
    }
    else if (section == 4)
    {
        label.text = sectionFourHeader;
    }
    else if (section == 5)
    {
        label.text = sectionFiveHeader;
    }    

    [headerView addSubview:label];
    return headerView;
}

Answer 1:

还有一大堆的状态维持在不必须是不同的实例变量。 我们可以用收藏等数据结构来建立的表是如何应该是一个定义,然后是指,在我们的数据源/委托方法。

此视图控制器的基本思想是,每个部分由a表示CVSectionModel包含该部分中的行的数据对象,并且特定部分的信息,例如首部视图。 里面viewDidLoad我们建立这些款机型,而我们完成更新我们的数据任何时候我们应该重新建立他们。 要尝试数据的工作,不同的安排,取代在调用viewDidLoadtestCase1与其它测试方法之一。

我们维持部分车型的阵列且仅当有要显示的内容有创建一个部分的模型。 正如你可以看到这简化了获得行的信息出来的又有多少行去每个部分是什么阵的计算。 我们只是要求部分型号​​的部分做什么,而不是同步多个不同的实例变量。

请注意,这台控制器不支持内容插入,删除或重新排序,因此,如果这是一个要求,将需要额外的工作更复杂的动画。 它在更新后支持常规表重新加载,不过,因为可以在viewDidLoad中10秒之后运行的块中可以看出。

请检查代码并询问任何问题。 这是实现文件,但是什么也没有在标题中除@interface CVViewController: UIViewController @end

@interface CVSectionModel : NSObject

@property (nonatomic) NSArray *rowModels;
@property (nonatomic) NSString *sectionTitle;
@property (nonatomic) NSInteger tag; // possibly useful for animations, different behavior on section, etc.

@end

@implementation CVSectionModel

@end


@interface CVViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic) NSArray *sectionModels;

@property (nonatomic) NSArray *sevenDays;
@property (nonatomic) NSArray *fourteenDays;
@property (nonatomic) NSArray *thirtyDays;

@end

@implementation CVViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // In your application, you'll be loading and processing your real data
    // But here I'm just going to simulate a number of cases with strings for illustration
    // Call the other methods to try different configurations
    [self testCase1];

    // We call this once on viewDidLoad. You'll also want to call it whenever you update the underlying data, as in the block below
    [self processSectionData];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"IDENTIFIER"];

    // Demonstrate support for modifying the underlying data and refresh the view.
    // Uncomment to test it.
//    double delayInSeconds = 10.0;
//    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
//    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//        [self testCase2];
//        [self processSectionData];
//        [self.tableView reloadData];
//    });
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [((CVSectionModel *)self.sectionModels[section]).rowModels count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IDENTIFIER"];
    // In your app this appears to be an NSManagedObject of some type, here it's just a string
    NSString *modelForRow = ((CVSectionModel *)self.sectionModels[indexPath.section]).rowModels[indexPath.row];
    cell.textLabel.text = modelForRow;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle = ((CVSectionModel *)self.sectionModels[section]).sectionTitle;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 150)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 25)];
    label.backgroundColor = [UIColor clearColor];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    label.textColor = [UIColor whiteColor];
    label.text = sectionTitle;
    [headerView addSubview:label];
    return headerView;
}

- (void)processSectionData
{
    NSArray *allDayArrays = @[self.sevenDays, self.fourteenDays, self.thirtyDays];
    // You won't use hard-coded strings here - use your sevenDaysSectionLabel, etc. variables instead
    NSArray *labelTitles = @[@"7 days", @"14 days", @"30 days"];
    NSMutableArray *mutableSectionModels = [NSMutableArray array];

    [allDayArrays enumerateObjectsUsingBlock:^(NSArray *array, NSUInteger index, BOOL *stop) {
        if ([array count] > 0) {
            CVSectionModel *sectionModel = [[CVSectionModel alloc] init];
            sectionModel.rowModels = array;
            sectionModel.sectionTitle = labelTitles[index];
            sectionModel.tag = index+1; // track this to enable re-ordering in the future
            [mutableSectionModels addObject:sectionModel];
        }
    }];

    // If Animating insertions / deletions will require something more elaborate
    self.sectionModels = [mutableSectionModels copy];
}

// Case 1:
// All three populated
- (void)testCase1
{
    self.sevenDays = @[@"sevendayitem1", @"sevendayitem2", @"sevendayitem3", @"sevendayitem4"];
    self.fourteenDays = @[@"14day item 1", @"14 day item 2"];
    self.thirtyDays = @[@"30 day item 1", @"30 day item 2", @"30 day item 3"];
}

// Case 2:
// self.fourteenDays is empty
- (void)testCase2
{
    self.sevenDays = @[@"sevendayitem1", @"sevendayitem2", @"sevendayitem3", @"sevendayitem4"];
    self.fourteenDays = @[];
    self.thirtyDays = @[@"30 day item 1", @"30 day item 2", @"30 day item 3"];
}

// Case 3:
// self.thirtydays is the only one
- (void)testCase3
{
    self.sevenDays = @[];
    self.fourteenDays = @[];
    self.thirtyDays = @[@"30 day item 1", @"30 day item 2", @"30 day item 3"];
}

@end


Answer 2:

我认为你必须确保你设置适当的

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;  

如果你想拥有多区域,并在各部分的多行,请使用NSDictionary中,并设置键 - >数组值,然后

NSDictionary *dict = @{@"section1": @[@"obj1", @"obj2"],@"section2": @[@"obj3", @"obj4"]};
NSInteger numberOfSections = [[dict allKeys] count];// return in numberOfSections
NSArray *values = [dict valueForKey:@"obj1"];// get appropriate array


文章来源: iOS: UITableViewController — cellForRowAtIndexPath cell creation