最后一个单元格在滚动时重复出现

时间:2015-02-16 07:09:52

标签: ios objective-c uitableview

在我的应用程序中,我在Tableview的末尾有一个单元格。在滚动时,它会重复出现。我正在使用基于XIB的自定义单元格。 After scrolling last cell gets repeated

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section
{    
 if([self.invoiceList count] > 0)
 {
         return [self.invoiceList count]+1;
}
else
{
    return [self.invoiceList count];
}
return 0;
}


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

InvoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    // Load the top-level objects from the custom cell XIB.
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"InvoiceTableViewCell" owner:self options:nil];
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
    cell = [topLevelObjects objectAtIndex:0];

}
   [self cellForTableView:tableView indexPathForCell:indexPath andInvoiceTableCell:cell];


return cell;
}

- (void)cellForTableView:(UITableView*)tableView indexPathForCell:(NSIndexPath *)indexPath andInvoiceTableCell:(InvoiceTableViewCell *)cell
{
//For displaying < END OF LIST >
if(indexPath.row == [self.invoiceList count])
{
    cell.lblEndOfListOL.hidden = NO;
    cell.lblItemOL.hidden = YES;
    cell.lblDescriptionOL.hidden = YES;
    cell.lblQuantityOL.hidden = YES;
    cell.lblUOMOL.hidden = YES;
    cell.imgViewCellVerticalOL1.hidden = YES;
    cell.imgViewCellVerticalOL2.hidden = YES;
    cell.imgViewCellVerticalOL3.hidden = YES;

}

//For Content cells
if([self.invoiceList count] > indexPath.row)
{
    [self setValuesForCell:cell fromArray:self.invoiceList forIndex:indexPath.row];
}

}

- (void)setValuesForCell:(InvoiceTableViewCell*)cell fromArray:(NSMutableArray*)array forIndex:(NSInteger)index
{
 //    self.invoiceTableViewCellOL = cell;
if(showLess)
{
    // First time load
    self.imgViewVertical1OL.hidden = YES;
    self.lblItemHeaderOL.hidden = YES;

    cell.lblItemOL.hidden = YES;
    cell.imgViewCellVerticalOL1.hidden = YES;

    //Manage view
    cell.lessCellView.hidden = false;
    cell.moreCellView.hidden = true;

    if(IS_IPHONE5_LANDSCAPE)
    {
        self.lessHeaderView.hidden = false;
        self.lessHeaderView.layer.opacity = 1;
        self.detailedHeaderView.hidden = true;
        self.detailedHeaderView.layer.opacity = 0;
    }
    else
    {
        // Headers            
        cell.frame = CGRectMake(0,0,474,35);
        self.lblDescriptionHeaderOL.frame = CGRectMake(0,45,95+9+195,35);

        // Values
        cell.lblItemOL.frame = CGRectMake(0,0,95,35);
        cell.descriptionLbl.frame = CGRectMake(0,0,95+9+195,35);
        cell.lblQuantityOL.frame = CGRectMake(308,0,75,35);
        cell.lblUOMOL.frame = CGRectMake(390,0,82,35);
        cell.imgViewCellVerticalOL1.frame = CGRectMake(95,-1,9,37);
        cell.imgViewCellVerticalOL2.frame = CGRectMake(299,-1,9,37);
        cell.imgViewCellVerticalOL3.frame = CGRectMake(383,-1,9,37);

    }

}
else
{
    self.imgViewVertical1OL.hidden = NO;
    self.lblItemHeaderOL.hidden = NO;

    cell.lblItemOL.hidden = NO;
    cell.imgViewCellVerticalOL1.hidden = NO;

    cell.lessCellView.hidden = true;
    cell.moreCellView.hidden = false;

    if(IS_IPHONE5_LANDSCAPE)
    {
        self.lessHeaderView.hidden = true;
        self.lessHeaderView.layer.opacity = 0;
        self.detailedHeaderView.hidden = false;
        self.detailedHeaderView.layer.opacity = 1;
    }
    else
    {
        // Headers
        cell.frame = CGRectMake(0,0,474,35);
        self.lblItemHeaderOL.frame = CGRectMake(3,45,95,35);
        self.lblDescriptionHeaderOL.frame = CGRectMake(107,45,195,35);

        // Values
        cell.lblItemOL.frame = CGRectMake(0,0,95,35);
        cell.lblDescriptionOL.frame = CGRectMake(104,0,195,35);
        cell.lblQuantityOL.frame = CGRectMake(308,0,75,35);
        cell.lblUOMOL.frame = CGRectMake(390,0,82,35);
        cell.imgViewCellVerticalOL1.frame = CGRectMake(95,-1,9,37);
        cell.imgViewCellVerticalOL2.frame = CGRectMake(299,-1,9,37);
        cell.imgViewCellVerticalOL3.frame = CGRectMake(383,-1,9,37);
    }

}

if (array.count > index) {
    if (!showLess) {
    cell.lblDescriptionOL.text = [NSString stringWithFormat:@"%@",[[array objectAtIndex:index]objectForKey:@"Description"]];
    }else {
    cell.descriptionLbl.text = [NSString stringWithFormat:@"%@",[[array objectAtIndex:index]objectForKey:@"Description"]];
    }
    cell.lblItemOL.text = [NSString stringWithFormat:@"%@",[[array objectAtIndex:index]objectForKey:@"ItemID"]];
    cell.lblQuantityOL.text = [NSString stringWithFormat:@"%@",[[array objectAtIndex:index]objectForKey:@"QtyShip"]];
    cell.lblUOMOL.text = [NSString stringWithFormat:@"%@",[[array objectAtIndex:index]objectForKey:@"UOMDetails"]];
}

}

建议我..谢谢

3 个答案:

答案 0 :(得分:0)

我认为您应该在行数中使用[self.invoiceList count]而不是[self.invoiceList count]+1;

答案 1 :(得分:0)

我假设通过滚动来改变stwitch状态,如果这样,下面的代码应该给你解决方案。

重复使用单元格对象。要在lblDescriptionOL和descriptionLbl之间加入值,你需要重置......

 if (array.count > index) {
   if (!showLess) {
    cell.lblDescriptionOL.text = [NSString stringWithFormat:@"%@",[[array objectAtIndex:index]objectForKey:@"Description"]];
cell.descriptionLbl.text = nil;
    }else {
    cell.descriptionLbl.text = [NSString stringWithFormat:@"%@",[[array objectAtIndex:index]objectForKey:@"Description"]];
cell.lblDescriptionOL.text = nil;
    }

答案 2 :(得分:0)

通过简单地为UITableView添加Footer视图而不是添加虚拟单元格来解决它。

self.invoiceTableViewOL.tableFooterView = [self getFooterView];

- (UIView *)getFooterView
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.invoiceTableViewOL.frame.size.width, 40.0f)];
    footerView.backgroundColor = [UIColor clearColor];
    UILabel *endLabel = [[UILabel alloc]init];
    endLabel.frame = footerView.frame;
    endLabel.textColor = [UIColor whiteColor];
    endLabel.textAlignment = UITextAlignmentCenter;
    endLabel.font = [UIFont systemFontOfSize:14.0f];
    endLabel.text = @"End Of List";

    [footerView addSubview:endLabel];
    endLabel = nil;

    return footerView;
    footerView = nil;
}