Last TableView Cell无法正常工作

时间:2012-10-28 19:13:08

标签: iphone ios xcode uitableview tableview

我会尽力解释这一点,并希望有人能够理解正在发生的事情:

我正在使用核心数据将数据加载到UITableView中。我试图在TableView的末尾添加一个“最后一个单元格”来显示徽标。这是我的代码:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  numberOfRows = section;
return     [[[self.fetchedResultsController sections] objectAtIndex:numberOfRows] numberOfObjects]+1;

}



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

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[ReminderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cellBackground.png"]];

row = indexPath.row;


if (row == [[[self.fetchedResultsController sections] objectAtIndex:numberOfRows] numberOfObjects]) {
    NSLog(@"%u",[[[self.fetchedResultsController sections] objectAtIndex:numberOfRows] numberOfObjects]);
    cell.productLabel.text = nil;
    cell.companyLabel.text = nil;
    cell.dateLabel.text = nil;
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.logo.hidden = NO;
    cell.renewLabel = nil;


}

else {

    Reminder *reminder = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.productLabel.text = reminder.product;
    cell.companyLabel.text = reminder.company;
    cell.dateLabel.text = reminder.date;
    cell.logo.hidden = YES;

}

return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    row = [indexPath row];

if (row == [[[self.fetchedResultsController sections] objectAtIndex:numberOfRows] numberOfObjects]) {
    return 50;
}

return 78;

}

正如你所知道的那样,我告诉最后一个单元格没有附件类型,而所有其他单元格都有一个。

正常细胞w /附件箭头: Has Accessory Arrow

最后一个单元格: Last Cell

一切都很美妙,直到我向下滚动到最后一个单元格的底部,然后向上滚动。配件箭头在一些应该有它的表格单元格中消失! (但不是全部)

相同的细胞,但箭已消失: Has No Arrow

这里发生了什么?

4 个答案:

答案 0 :(得分:2)

这是因为排队机制。 tableView从其缓存中取消任意单元格。如果这些单元格恰好是您之前设置的单元格之一:

cell.accessoryType = UITableViewCellAccessoryNone;

它没有附件指示器。解决这个问题的最简单方法(在我看来,当然你也可以使用不同的重用标识符)就是修改你的代码:

else {

    Reminder *reminder = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.productLabel.text = reminder.product;
    cell.companyLabel.text = reminder.company;
    cell.dateLabel.text = reminder.date;
    cell.logo.hidden = YES;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}

答案 1 :(得分:1)

问题是dequeReusableCellWithIdentifier:的调用,因为所有单元格都具有相同的标识符。这意味着当“最后一个单元格”出现然后滚动时,它是可重复使用单元格的下一个提示,并且因为所有单元格都具有相同的标识符,所以每当其中一个单元格从屏幕上掉落时,下一个一个人将没有配件。您需要提出一个实现,为“最后一个单元格”提供不同的标识符。

答案 2 :(得分:1)

我会这样做,定义两个CellIdentifier,一个用于普通单元格,一个用于最后一个单元格:

static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifierBottom = @"CellBottom";

如果建设,请创建你的单元格:

if (row != lastTableViewIndex) {
    //create cell with CellIdentifier
} else {
    //create cell with CellIdentifierBottom
}

答案 3 :(得分:0)

您可以尝试使用下面的UITableViewDelegate回调功能在显示单元格后自定义您的单元格:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row != (yourDataSource.count-1) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}
相关问题