我在表中显示数据时遇到问题,在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;
}
答案 0 :(得分:5)
问题可能在这里:
...
[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;
答案 1 :(得分:0)
很久以前我遇到了同样的问题。这是它的解决方案。 在添加新标签之前,您需要删除现有标签,因为新标签在现有标签上重叠。
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UILabel *subview in subviews) {
[subview removeFromSuperview];
}
[subviews release];
快乐的编码...... 保持饥饿,保持愚蠢。
答案 2 :(得分:0)
我的问题是我在一个视图控制器(tableviews的轮播)中有9个tableview,并且正在为所有这些使用一个单元标识符。我通过为每个tableview单元格分配单独的标识符(在我的例子中为9,每个tableview的不同标识符)解决了这个问题。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ProjectTableviewCell";
TimesheetTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"ProjectTableviewCell %li",tableView.tag]];
if (!cell) {
cell = [[TimesheetTableviewCell alloc] initWithReuseIdentifier:CellIdentifier];
}
TimesheetWeek *week = [LoginVC getTimesheetWeeks][tableView.tag];
Project *project = week.currentData[indexPath.row];
[cell setCellProject:project];
cell.delegate = self;
return cell;
}