无法在表视图中重用自定义单元格

时间:2012-03-06 05:11:13

标签: iphone ios

我创建了一个包含标签和文本字段的自定义单元格。我在表格视图中使用此自定义单元格。当我在文本字段中输入数据时,当我滚动视图时,我的数据正在被替换。对于每个滚动它被替换/擦除。任何一个告诉我我在哪里做错。 我的自定义单元格代码就在这里。谢谢!

-(UITableViewCell *)returnCellForEach:(UITableView *)tableView
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];
    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCellToAddDetails" owner:self options:nil];
        cell = customCell;
//        customCell = nil;
        customLabel = (UILabel *)[customCell.contentView viewWithTag:100];
    }
    return cell;
}

-(UITextField *)dataField
{
    UITextField *textField = customField;

    return textField;
}

4 个答案:

答案 0 :(得分:0)

您需要存储用户输入的与特定索引路径相关的数据。当您滚动单元格时,首先需要检查是否存在与该索引路径对应的任何dataObject,并相应地设置数据。您可以使用字典存储与特定单元格对应的数据。希望它有所帮助

答案 1 :(得分:0)

我不熟悉您正在使用的方法名称,在我的情况下,我需要在tableview类之外创建自定义表视图Cell Class,

该类称为QRTYPECELL,以下是相关代码:

@implementation QRTypeCell
@synthesize imageView;
@synthesize labelview;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    imageView = nil;
    labelview= nil;
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,30,30)];
    imageView.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:imageView];
    self.contentView.backgroundColor = [UIColor blackColor];


    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.textColor = [UIColor greenColor];
    label.backgroundColor = [UIColor blackColor];

    label.font = [UIFont systemFontOfSize:17];
    self.labelview = label;
    [self.contentView addSubview:label];


}
return self;}

然后在TableView类中:

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

QRTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[QRTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
    CurrentQRCode = (QRCode *)[fetchedResultsController objectAtIndexPath:indexPath];
    NSString *icon = CurrentQRCode.parsed_icon;
    NSString *string = CurrentQRCode.parsed_string;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"]; 
    UIImage *image_file = [UIImage imageWithContentsOfFile:filePath];

答案 2 :(得分:0)

在IB中,您会看到标识符字段中的任何值,并将该值用作重用标识符。 我认为这会对你有所帮助。

答案 3 :(得分:0)

好吧,我知道你为customCell创建了类并替换了这行

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];

CustomCell *customCell=(CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
相关问题