tableView在10个单元格后重新使用indexPath

时间:2015-10-11 17:58:30

标签: ios objective-c uitableview

这是我之前提出的问题的后续行动。我改变了代码以使用不可重复使用的单元格,认为单元格重复使用会重复我的计时器。它仍然发生。我做了tableView Cells的NSLog,从列表中你可以看到indexPath在10或11个单元后开始重复。为什么会这样?您还可以看到应用程序在第10行进入后台模式,因为消息"已保存所有BNR项目"出现。我发誓我没有点击主页按钮。但这似乎是tableView单元重用的原因:恢复代码无法正常工作,因此indexPath重置为0.

新代码:

    #pragma mark -tableView

// **** changelog 10/2/2015 ***************************************
// alternates blue and white rows

- (void)tableView: (UITableView*)tableView
  willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
    cell.backgroundColor = indexPath.row % 2
    ? [UIColor colorWithRed: 0.0 green: 0.3 blue: 1.0 alpha: 0.3]
    : [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
// *********************************************************************
-(NSInteger) tableView:(UITableView *)tableView numberOfSectionsInTableview:(UITableView *)tableview{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[NRCItemStore sharedStore] allItems] count];
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"NRCItemCell"];
    // Get a new or recycled cell
    //NRCItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRCItemCell" forIndexPath:indexPath];

    self.timerItem.cell = cell;
    [self handleTimer];
    NSLog(@"cell for row at index %@ is %@", indexPath, cell);
    return cell;
}

    2015-10-11 12:41:06.398 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1740b9f20>
2015-10-11 12:41:06.409 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0} is <UITableViewCell: 0x14d52e340; frame = (0 0; 320 44); text = 'panda'; layer = <CALayer: 0x17022e6c0>>
2015-10-11 12:41:16.837 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1702a5580>
2015-10-11 12:41:16.840 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000008016> {length = 2, path = 0 - 1} is <UITableViewCell: 0x14d5353a0; frame = (0 0; 320 44); text = 'cub'; layer = <CALayer: 0x17022fb00>>
2015-10-11 12:41:29.777 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1740bb660>
2015-10-11 12:41:29.780 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000010016> {length = 2, path = 0 - 2} is <UITableViewCell: 0x14d63c0d0; frame = (0 0; 320 44); text = 'bob'; layer = <CALayer: 0x1742311c0>>
2015-10-11 12:41:44.201 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1702a8f40>
2015-10-11 12:41:44.204 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000018016> {length = 2, path = 0 - 3} is <UITableViewCell: 0x14d536810; frame = (0 0; 320 44); text = 'carol'; layer = <CALayer: 0x170224e20>>
2015-10-11 12:41:55.906 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1702aae00>
2015-10-11 12:41:55.909 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000020016> {length = 2, path = 0 - 4} is <UITableViewCell: 0x14d639ec0; frame = (0 0; 320 44); text = 'Ted'; layer = <CALayer: 0x17422d180>>
2015-10-11 12:42:08.346 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1740be000>
2015-10-11 12:42:08.349 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000028016> {length = 2, path = 0 - 5} is <UITableViewCell: 0x14d50c230; frame = (0 0; 320 44); text = 'Alice'; layer = <CALayer: 0x170230120>>
2015-10-11 12:42:20.786 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1702a8d60>
2015-10-11 12:42:20.789 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000030016> {length = 2, path = 0 - 6} is <UITableViewCell: 0x14d51ab40; frame = (0 0; 320 44); text = 'John'; layer = <CALayer: 0x17003aa40>>
2015-10-11 12:42:32.058 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1702aac80>
2015-10-11 12:42:32.061 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000038016> {length = 2, path = 0 - 7} is <UITableViewCell: 0x14d63ea20; frame = (0 0; 320 44); text = 'Mary'; layer = <CALayer: 0x17422c9e0>>
2015-10-11 12:42:49.917 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1740be420>
2015-10-11 12:42:49.920 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000040016> {length = 2, path = 0 - 8} is <UITableViewCell: 0x14d63f660; frame = (0 0; 320 44); text = 'sue'; layer = <CALayer: 0x17403aba0>>
2015-10-11 12:43:03.588 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1740be840>
2015-10-11 12:43:03.590 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000048016> {length = 2, path = 0 - 9} is <UITableViewCell: 0x14d636d40; frame = (0 0; 320 44); text = 'Joan'; layer = <CALayer: 0x17422cce0>>
2015-10-11 12:43:24.583 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1702a8ee0>
2015-10-11 12:43:24.586 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000050016> {length = 2, path = 0 - 10} is <UITableViewCell: 0x14d642ad0; frame = (0 0; 320 44); text = 'sarah'; layer = <CALayer: 0x174230740>>
2015-10-11 12:43:45.782 timeLogger[1447:760103] Saved all of the BNRItems
2015-10-11 12:44:15.109 timeLogger[1447:760103] This was returned from detailViewController <NRCtimerItem: 0x1740b9fe0>
2015-10-11 12:44:15.114 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000058016> {length = 2, path = 0 - 11} is <UITableViewCell: 0x14d646c60; frame = (0 0; 320 44); text = 'Pete'; layer = <CALayer: 0x174230fc0>>
2015-10-11 12:44:17.629 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000058016> {length = 2, path = 0 - 11} is <UITableViewCell: 0x14d642fd0; frame = (0 0; 320 44); text = 'Joan'; layer = <CALayer: 0x17422df00>>
2015-10-11 12:44:19.520 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0} is <UITableViewCell: 0x14d622a20; frame = (0 0; 320 44); text = 'Joan'; layer = <CALayer: 0x17422e7c0>>
2015-10-11 12:44:30.359 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0} is <UITableViewCell: 0x14d53b4f0; frame = (0 0; 320 44); text = 'Joan'; layer = <CALayer: 0x17003a800>>
2015-10-11 12:44:30.943 timeLogger[1447:760103] cell for row at index <NSIndexPath: 0xc000000000050016> {length = 2, path = 0 - 10} is <UITableViewCell: 0x14d509080; frame = (0 0; 320 44); text = 'Joan'; layer = <CALayer: 0x170232ca0>>

0 个答案:

没有答案