0

我有一个程序,其中UITableView包含从笔尖加载的自定义单元格。这些单元格具有文本字段和UIImage. 我一直在将它们包含的信息传递给自定义类,并对类进行编码/解码以实现数据持久性。当我想加载数据时,我将类中的信息放入单元格中。这适用于 1 个单元格,但不适用于多个单元格。我已经检查过了,并且这些类正在正确地写入文件。

这是我的检索方法:

//Fills an array if the file exists, otherwise returns nil
- (NSMutableArray*) findFile: (NSString *) add
{
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self saveFilePath:add]])
    {
        NSString *temp = [add stringByAppendingString:@"dat"];
        namesIndexer = [[NSMutableArray alloc] initWithContentsOfFile:[self saveFilePath:temp]];

        if (namesIndexer == nil) return nil;

        NSMutableArray *thing = [NSMutableArray new];
        for (NSString *place in namesIndexer)
        {
            temp = [add stringByAppendingString:place];
            PTextHolder *p = [NSKeyedUnarchiver unarchiveObjectWithFile:[self saveFilePath:temp]];
            [thing addObject:p];
        }
        return thing;
    }
    else 
    {
        return nil;
    }
}

请注意,这是在不同的类中,它从持有者那里调用方法。

//Returns a cell to be used at a row, populates it from the holder object
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    static NSString *personCellId = @"personID";
    UINib *nib = [UINib nibWithNibName:@"PersonCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:personCellId];
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:personCellId];

    cell.owner = tableView;
    if (mineTable == nil) mineTable = tableView;
    cell.delegated = formDataStorage;

    [formDataStorage putWhatShouldBeInThisCellForThisRowInIt:cell:(int*)indexPath.row];

    cell.currentRow = [[NSNumber alloc] initWithInt:indexPath.row];

    return cell;
}

这是它调用的方法:

- (void) putWhatShouldBeInThisCellForThisRowInIt: (PersonCell *) someCell: (int *) someRow
{
    if ((NSUInteger) someRow >= cake.count)
    {
        NSLog(@"The cake has been undercooked");
        return;
    }

    PTextHolder *temp = [cake objectAtIndex:(NSUInteger) someRow];

    someCell.firstName.text = temp.first;
    someCell.lastName.text = temp.last;
    someCell.middleName.text = temp.middle;
    someCell.suffixName.text = temp.suffix;
    someCell.email.text = temp.email;
    someCell.theSignature.image = temp.sig;
}

这里看起来有什么问题/会导致只加载一个单元格吗?

4

1 回答 1

1

我会首先检查数组中的项目数

[数组计数]

,如果项目数等于 1,那么问题就如你所猜测的编码/解码。

如果不是,您的代码是正确的,问题在于您的代码加载单元格。

顺便说一句,为什么不直接使用以下方法存储“cellInfoClass”数组:

[NSKeyedArchiver archiveRootObject:array toFile:filePath]

并直接检索数组。

我猜你已经在你的类中添加了编码/编码代码,如果不是这样:

/**  
 * Returns an object initialized from data in a given unarchiver. (required)  
 *  
 * @param decoder: An unarchiver object.  
 */
- (id)initWithCoder:(NSCoder *)coder {
    if (self = [super init]) {      
    // If parent class also adopts NSCoding, replace [super init]
    // with [super initWithCoder:decoder] to properly initialize.       
       [self setName:[coder decodeObjectForKey:@"name"]];       
       [self setId:[coder decodeIntForKey:@"id"]];      
       [self setDomain:[coder decodeObjectForKey:@"domain"]];   
    }
    return self;     
 }


/**  
 * Encodes the receiver using a given archiver. (required) 
 * @param coder: An archiver object.  
 */
- (void)encodeWithCoder:(NSCoder *)coder{
    // If parent class also adopts NSCoding, include a call to
    // [super encodeWithCoder:encoder] as the first statement.  
    [coder encodeObject:name forKey:@"name"];
    [coder encodeInt:id forKey:@"id"];
    [coder encodeObject:domain forKey:@"domain"];
}
于 2012-06-25T17:47:38.350 回答