我在显示表中的数据时遇到问题,在更新Tebel时调用方法[tv reloadData],但是当我选择一行时,它将显得模糊,好像以前的数据没有被删除。我认为问题出在存储SQL查询带来的数据的类中,但是我在所有字段中都有一个[release],但是没有用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
iMAPArrayProdutos *ArrayProdutos = (iMAPArrayProdutos *)[iMAP objectAtIndex:indexPath.row];
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
iMAPTabela *cell = (iMAPTabela *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
tv.autoresizesSubviews = YES;
if (cell == nil) {
cell = [[[iMAPTabela alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 50.0, tableView.rowHeight)] autorelease];
[cell addColumn:70];
label.tag = TAG_1;
label.font = [UIFont systemFontOfSize:12.0];
label.text = ArrayProdutos.Cod;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(80.0, 0, 50.0, tableView.rowHeight)] autorelease];
[cell addColumn:140];
label.tag = TAG_2;
label.font = [UIFont systemFontOfSize:12.0];
label.text = ArrayProdutos.Artrf2;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(145.0, 0, 100.0, tableView.rowHeight)] autorelease];
[cell addColumn:260];
label.tag = TAG_3;
label.font = [UIFont systemFontOfSize:12.0];
label.text = ArrayProdutos.Descri;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
return cell;
}
直接从银行执行查询:
- (void)readiMAPFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the Array
iMAP = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
sqlite3_stmt *compiledStatement;
const char *sqlStatement;
if ([sb.text length] == 0) {
sqlStatement = "SELECT * FROM APSB1010";
}
else {
NSString *string1;
if ([OpcaoFiltro isEqualToString:@"1"]) {
string1 = @"SELECT * FROM APSB1010 WHERE TRIM(COD) LIKE '%";
}
else if ([OpcaoFiltro isEqualToString:@"2"]) {
string1 = @"SELECT * FROM APSB1010 WHERE TRIM(ARTRF2) LIKE '%";
}
else if ([OpcaoFiltro isEqualToString:@"3"]) {
string1 = @"SELECT * FROM APSB1010 WHERE TRIM(DESCRI) LIKE '%";
}
else if ([OpcaoFiltro isEqualToString:@"4"]) {
string1 = @"SELECT * FROM APSB1010 WHERE TRIM(APLICA) LIKE '%";
}
else {
string1 = @"SELECT * FROM APSB1010 WHERE TRIM(APLICA) LIKE '%";
}
NSString *string2 = @"%'";
NSString *result = [[NSString alloc] init];
result = [result stringByAppendingString:string1];
result = [result stringByAppendingString:sb.text];
result = [result stringByAppendingString:string2];
sqlStatement = [result UTF8String];
}
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *ProdCod = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *ProdPrv1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *ProdGrupo = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *ProdPicment = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSString *ProdDescri = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSString *ProdArtrf2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
NSString *ProdUm = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
NSString *ProdEmbap = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];
NSString *ProdDesmax = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];
NSString *ProdImgap = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)];
NSString *ProdAplica = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)];
NSString *ProdQatu_01 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 11)];
NSString *ProdQatu_11 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 12)];
NSString *ProdQatu_12 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 13)];
NSString *ProdQatu_13 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 14)];
NSString *ProdQatu_14 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 15)];
NSString *ProdPrprom = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 16)];
NSString *ProdR_E_C_N_O_ = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 17)];
// Create a new object with the data from the database
iMAPArrayProdutos *ArrayProdutos = [[iMAPArrayProdutos alloc] Cod:ProdCod Prv1:ProdPrv1 Grupo:ProdGrupo Picment:ProdPicment Descri:ProdDescri Artrf2:ProdArtrf2 Um:ProdUm Embap:ProdEmbap Desmax:ProdDesmax Imgap:ProdImgap Aplica:ProdAplica Qatu_01:ProdQatu_01 Qatu_11:ProdQatu_11 Qatu_12:ProdQatu_12 Qatu_13:ProdQatu_13 Qatu_14:ProdQatu_14 Prprom:ProdPrprom R_E_C_N_O_:ProdR_E_C_N_O_];
// Add the object to the Array
[iMAP addObject: ArrayProdutos];
[ArrayProdutos release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
[tv reloadData];
}
我存储查询带来的数据的类:
@implementation iMAPArrayProdutos
@synthesize Cod;
@synthesize Prv1;
@synthesize Grupo;
@synthesize Picment;
@synthesize Descri;
@synthesize Artrf2;
@synthesize Um;
@synthesize Embap;
@synthesize Desmax;
@synthesize Imgap;
@synthesize Aplica;
@synthesize Qatu_01;
@synthesize Qatu_11;
@synthesize Qatu_12;
@synthesize Qatu_13;
@synthesize Qatu_14;
@synthesize Prprom;
@synthesize R_E_C_N_O_;
-(id)Cod:(NSString *)ProdCod Prv1:(NSString *)ProdPrv1 Grupo:(NSString *)ProdGrupo Picment:(NSString *)ProdPicment Descri:(NSString *)ProdDescri Artrf2:(NSString *)ProdArtrf2 Um:(NSString *)ProdUm Embap: (NSString *)ProdEmbap Desmax:(NSString *)ProdDesmax Imgap:(NSString *)ProdImgap Aplica:(NSString *)ProdAplica Qatu_01:(NSString *)ProdQatu_01 Qatu_11:(NSString *)ProdQatu_11 Qatu_12:(NSString *)ProdQatu_12 Qatu_13:(NSString *)ProdQatu_13 Qatu_14:(NSString *)ProdQatu_14 Prprom:(NSString *)ProdPrprom R_E_C_N_O_:(NSString *)ProdR_E_C_N_O_ {
self.Cod = ProdCod;
self.Prv1 = ProdPrv1;
self.Grupo = ProdGrupo;
self.Picment = ProdPicment;
self.Descri = ProdDescri;
self.Artrf2 = ProdArtrf2;
self.Um = ProdUm;
self.Embap = ProdEmbap;
self.Desmax = ProdDesmax;
self.Imgap = ProdImgap;
self.Aplica = ProdAplica;
self.Qatu_01 = ProdQatu_01;
self.Qatu_11 = ProdQatu_11;
self.Qatu_12 = ProdQatu_12;
self.Qatu_13 = ProdQatu_13;
self.Qatu_14 = ProdQatu_14;
self.Prprom = ProdPrprom;
self.R_E_C_N_O_ = ProdR_E_C_N_O_;
return self;
}
最佳答案
问题可能在这里:
...
[cell.contentView addSubview:label];
...
您会不断向该单元格添加新标签,即使它已被重复使用(由
dequeueReusableCellWithIdentifier
返回)您应该在创建单元的块中添加标签。以后,如果单元被重用,只需使用分配的标签从单元中获取标签,然后根据新数据更改文本。
if (cell == nil) {
// this is invoked when dequeueReusableCell returns nil - we actually create the label
cell = [[[iMAPTabela alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 50.0, tableView.rowHeight)] autorelease];
[cell addColumn:70];
label.tag = TAG_1;
label.font = [UIFont systemFontOfSize:12.0];
label.text = ArrayProdutos.Cod;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
}
//this is invoked every time UITableView asks for a cell
UILabel *labelToChange = [cell.contentView viewWithTag:TAG_1];
labelToChange.text = text = ArrayProdutos.Cod;
关于objective-c - reloadData-UITableView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7515491/