我正在使用的一些东西存在一个奇怪的问题。自定义 tableViewCell 或 db.在我的应用程序中,我对 UITableViewCell 进行了子类化。我的 subview 不透明,因此不应出现滚动问题或对生产力的影响。它们看起来不错,唯一的问题是我只能看到前 10 个单元格,在我获得 NAME 标签的相同名称和其他重复名称之后,同时从数据库中获取正确的 id。看起来像
猫 id=12 >> 狗 id=18
代替 CAT 的应该是 DOG。单击 CAT 单元格,我得到 DOG 标题。所以它说,viewCell 的标签更安全。
我尝试在 SQLiteStudio 中运行相同的数据库选择,它们没问题。我正在获取全套记录。有人遇到同样的问题吗?问题出在哪里?
DBAccess 方法:
-(NSMutableArray*)getNominals:(int)subCountryID
{
NSMutableArray *nominals=[[[NSMutableArray alloc]init]autorelease];
const char* sqlNominals=sqlite3_mprintf("SELECT noms.nominalID, noms.nominal,noms.nominalName,noms.nominalImg,noms.priority\
FROM nominals AS noms\
INNER JOIN NominalsAndSubCountriesRelation as rel\
ON noms.nominalID=rel.NominalID\
WHERE rel.SubcountruID=%i\
ORDER BY noms.priority",subCountryID);
sqlite3_stmt *statement;
int sqlResult = sqlite3_prepare_v2(database, sqlNominals, -1, &statement, NULL);
if ( sqlResult== SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
Nominal *nom=[[Nominal alloc]init];
nom.nominalID=sqlite3_column_int(statement, 0);
char *nominal=(char *)sqlite3_column_text(statement, 1);
char *nominalName=(char*)sqlite3_column_text(statement, 2);
char *nominalImg=(char*)sqlite3_column_text(statement, 3);
nom.nominal=(nominal)?[NSString stringWithUTF8String:nominal]: @"";
nom.nominalName=(nominalName)?[NSString stringWithUTF8String:nominalName]: @"";
nom.nominalImg=(nominalImg)?[NSString stringWithUTF8String:nominalImg]: @"";
[nominals addObject:nom];
[nom release];
}
sqlite3_finalize(statement);
}
else
{
[self dbConnectionError];
}
return nominals;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.nominalsArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NominalsCustomCell *cell =(NominalsCustomCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[NominalsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
Nominal *nominalsObj=[self.nominalsArr objectAtIndex:[indexPath row]];
[cell setTheNominals:nominalsObj];
}
return cell;
}
提前谢谢您。
最佳答案
移动以下代码
Nominal *nominalsObj=[self.nominalsArr objectAtIndex:[indexPath row]];
[cell setTheNominals:nominalsObj];
在 if (cell == nil)
block 之外,因为每当设置单元格时都应该设置它,无论是新的还是重复使用的单元格。在您的情况下,您仅为新创建的单元格设置数据 - 重用的单元格只需由 UITableView 调出并与最初设置的数据一起显示。
详细了解 UITableView 单元格重用的工作原理。
关于ios - 只有 10 个 CustomViewCells 是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8695130/