自定义单元被其他单元替换 [英] Custom cells getting replaced with other cells

查看:36
本文介绍了自定义单元被其他单元替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义单元格.当我在表格视图中使用这些自定义单元格时,我的单元格被替换了.我在表格视图中创建了大约10个部分,其中每个部分包含1行.我正在将自定义单元格"用于所有10个部分.当我滚动视图时,最后4个单元格将替换为顶部单元格.我正在使用ReuseIdentifier,但它们仍在被替换.有解决这个问题的想法吗?谢谢!

I created a custom cell. When I use these custom cells in table view My cells getting replaced.I created about 10 sections in the table view in which each section contains 1 row. I am using Custom Cells for all 10 sections. When I scroll the view, the last 4 cells are replaced with top cells. I am using ReuseIdentifier but they are still getting replaced. Any Ideas to fix this? Thanks!

这是我的CustomCell代码!

This is my CustomCell Code!

-(UITableViewCell *)returnCellForBirthdayDetails:(UITableView *)tableView
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellForBirthdayDetails"];

    if(cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCellToAddDetails" owner:self options:nil];
        cell = customCell;
        //        customCell = nil;
    }

    return  cell;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [customCellTextField resignFirstResponder];
    return YES;
}

- (UITextField *)textField
{
    UITextField *textField = nil;

    if( customCell )
    {
        textField = (UITextField *)[customCell.contentView viewWithTag:101];
    }

    return textField;
}

在这里,我在tableView中调用自定义单元格

and Here I am calling custom cells in the tableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      UITableViewCell *cell = nil;

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
     addCustomCell  = [[CustomCellToAddDetailsController alloc] init];

    switch (indexPath.section) 
    {
        case 0:
            cell = [addCustomCell returnCellForBirthdayDetails:tableView];
            addCustomCell.customCellLabel.text = @"FirstName";
            addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
            addCustomCell.customCellTextField.placeholder = @"FirstName";
            [delegate.fieldArray addObject:addCustomCell];
            break;
        case 1:
            cell = [addCustomCell returnCellForBirthdayDetails:tableView];
            addCustomCell.customCellLabel.text = @"LastName";
            addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
            addCustomCell.customCellTextField.placeholder = @"LastName";
            [delegate.fieldArray addObject:addCustomCell];
            break;
        case 2:
            cell = [addCustomCell returnCellForBirthdayDetails:tableView];
            addCustomCell.customCellLabel.text = @"Dob";
            addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
            addCustomCell.customCellTextField.placeholder = @"Dob";
            addCustomCell.customCellTextField.inputView = dp;
            addCustomCell.customCellTextField.inputAccessoryView=toolBarForTableView;
            [delegate.fieldArray addObject:addCustomCell];
            break;
        case 3:
            cell = [addCustomCell returnCellForBirthdayDetails:tableView];
            addCustomCell.customCellLabel.text = @"Address";
            addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
            addCustomCell.customCellTextField.placeholder = @"Enter Address";
            [delegate.fieldArray addObject:addCustomCell];
            break;
        case 4:
            cell = [addCustomCell returnCellForBirthdayDetails:tableView];
            addCustomCell.customCellLabel.text = @"City";
            addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
            addCustomCell.customCellTextField.placeholder = @"Enter City";
            [delegate.fieldArray addObject:addCustomCell];
            break;
        case 5:
            cell = [addCustomCell returnCellForBirthdayDetails:tableView];
            addCustomCell.customCellLabel.text = @"State";    
            addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
            addCustomCell.customCellTextField.placeholder = @"Enter State";
            [delegate.fieldArray addObject:addCustomCell];
            break;
        case 6:
            cell = [addCustomCell returnCellForBirthdayDetails:tableView];
            addCustomCell.customCellLabel.text = @"Email";
            addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
            addCustomCell.customCellTextField.placeholder = @"Enter Email";
            [delegate.fieldArray addObject:addCustomCell];
            break;
        case 7:
            cell = [addCustomCell returnCellForBirthdayDetails:tableView];
            addCustomCell.customCellLabel.text = @"Phone";
            addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
            addCustomCell.customCellTextField.placeholder = @"Enter MobileNo";
            [delegate.fieldArray addObject:addCustomCell];
            break;
    }
    return cell;
    [addCustomCell release];

}

请让我知道我在犯什么错误.谢谢!

Please let me know what mistake I am making. Thanks!!

推荐答案

为每个单元格使用不同的复用标识符.当您使用相同的标识符时,表视图将仅捕获具有该标识符的任何可用单元格(在您的情况下,它将捕获顶部的单元格,因为不再使用它们了.)

Use a different reuseIdentifier for each cell. When you use the same identifier, then the tableview will just grab any available cell with this identifier (in your case it's grabbing the top ones since they are no longer in use).

您可以对相同的单元格使用相同的标识符(并且应该使用),除了文本/图像之外,所有单元格都相同.然后,您可以调用"configureCell"方法,在其中为每个单元格设置文本/图像/等.

You can use the same identifier (and you should) for cells that are identical in everything except text/image. Then you can call a 'configureCell' method where you set the text/image/etc for each cell.

这篇关于自定义单元被其他单元替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆