我有一个程序,其中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;
}
这里看起来有什么问题/会导致只加载一个单元格吗?
答案 0 :(得分: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"];
}