reloadData - UITableView
I have a problem in displaying the data in a table, call the method [tv reloadData] when Tebel is updated but when I select a row it will appear blurred, as if the previous data had not been removed. I think the problem is in the class that stores the data brought by my SQL query, but I have one [release] in all the fields and did not work.
- (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;
}
Perform the query directly from the Bank:
- (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 = [r开发者_高级运维esult 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];
}
Class where I store the data brought by the query:
@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;
}
The problem is probably here:
...
[cell.contentView addSubview:label];
...
you continuously add new labels to the cell, even if it is reused (returned by dequeueReusableCellWithIdentifier
)
You should add your labels in the block where you create your cell. Later, if the cell is reused just get the labels from the cells using the assigned tags and change the text according to the new data.
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;
I got same problem long time ago. This is the solution of it. You need to remove existing labels before u add new ones, as new labels are overlapping on existing labels.
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UILabel *subview in subviews) {
[subview removeFromSuperview];
}
[subviews release];
Happy coding... Stay Hungry, Stay Foolish.
My problem was that I had 9 tableviews in one view controller (a carousel of tableviews) and was using one cell identifier for all of them. I resolved this issue by assigning separate identifiers(9,in my case, different identifiers for each of my tableviews) for each tableview cell.
- (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;
}
Screenshots before & after:
精彩评论