strange problem when placing UILabels on UITableView and calling reloadData
- by user262325
Hello everyone
I hope to list all files in a directory to an UITableView atableview
There are files in Document directory
a.txt
b.txt
F (folder)
c.txt
in folder F, there are 2 files
M.exe
N.exe
When I change to folder F, it should display on atableview
M.exe
N.exe
but Result is in F, it displayed
b.txt
F
the codes are shown as below
-(void) removeACell:(NSInteger)row;
{
NSUInteger _lastSection = 0;//[self numberOfSectionsInTableView:atableview];
NSUInteger _lastRow =row;// [atableview numberOfRowsInSection:_lastSection] - 1;
NSUInteger _path[2] = {_lastSection, _lastRow};
NSIndexPath *_indexPath = [[NSIndexPath alloc] initWithIndexes:_path length:2];
NSArray *_indexPaths = [[NSArray alloc] initWithObjects:_indexPath, nil];
[_indexPath release];
[atableview deleteRowsAtIndexPaths:_indexPaths withRowAnimation:UITableViewRowAnimationBottom];
[_indexPaths release];
}
-(void) reloadDirectory
{
if([fileArray count]>0)
{
NSInteger n=fileArray.count;
for(int i=n-1;i>=0;i--)
{ [fileArray removeObjectAtIndex: i];
[self removeACell:i];
}
}
NSFileManager *manager = [NSFileManager defaultManager];
NSLog(@"*******************" );
NSLog(@"ffff%@",[self appDelegate].gCurrentPath_AppDelegate);
for(NSString *path in [manager contentsOfDirectoryAtPath:[self appDelegate].gCurrentPath_AppDelegate
error:nil])
{
NSDictionary *modData= [manager attributesOfItemAtPath:
[appDelegate.gCurrentPath_AppDelegate //directory of files
stringByAppendingPathComponent:path ]
error:nil ];
NSDate * dateModified=(NSDate *) [modData objectForKey:NSFileModificationDate];
NSNumber *fileSize=[modData objectForKey:NSFileSize] ;
if(dateModified)
{
FileObj *newobj=[[FileObj alloc] init ];
[ newobj seta:1];
NSString *ss=[[NSString alloc] initWithFormat:@"%@",path] ;
[newobj setfileName:ss];
NSLog(@"---------------%@",ss);
BOOL isDir;
[manager fileExistsAtPath:[appDelegate.gCurrentPath_AppDelegate
stringByAppendingPathComponent:path ] isDirectory:&isDir];
[newobj setisDirectory: isDir ];
[newobj setfilePath:@"1"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *stringFromDate =[[NSString alloc] initWithString: [formatter stringFromDate:dateModified]];
[newobj setfileDate:stringFromDate];
[formatter release];
NSString * fileSize1= [[NSString alloc] initWithFormat:@"%d",[fileSize integerValue] ];
[newobj setfileSize: fileSize1];
[ fileArray addObject:newobj];
[newobj release];
};
[atableview reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ NSInteger row=[indexPath row];
NSInteger section=[indexPath section];
static NSString *SimpleTableIdentifier1 = @"CellTableIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier1 ];
int newRow = [indexPath row];
selectIndex=newRow;
if(cell==nil)
{
//**breakpoint AAA here**
CGRect cellframe=CGRectMake(0, 0, 300, 60);
cell=[[[UITableViewCell alloc] initWithFrame:cellframe
reuseIdentifier:SimpleTableIdentifier1] autorelease];
if ([[fileArray objectAtIndex:row] getisDirectory])
{
UIImage *image = [UIImage imageNamed:@"folder.png"];
cell.imageView.image = image;
}
else
{
UIImage *image = [UIImage imageNamed:@"files.png"];
cell.imageView.image = image;
}
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
///--------------------------------------------------------
UILabel *b1 =[ [UILabel alloc]init];
b1.frame = CGRectMake(40.9f,8.0f,250.0f,20.0f) ;
[b1 setBackgroundColor:[UIColor clearColor]];
[b1 setTag:(row+15000)];
//-------------------------------------------------------
UILabel *b2 =[ [UILabel alloc]init];
b2.frame = CGRectMake(40.9f,30.0f,250.0f,13.0f) ;
[b2 setBackgroundColor:[UIColor clearColor]];
[b2 setTag:row+16000];
[b2 setFont:[UIFont systemFontOfSize:10.0]];
//------------------------------------------------
UILabel *b3 =[ [UILabel alloc]init];
b3.frame = CGRectMake(40.9f,45.0f,250.0f,13.0f) ;
[b3 setBackgroundColor:[UIColor clearColor]];
[b3 setFont:[UIFont systemFontOfSize:10.0]];
[b3 setTag:row+17000];
[cell.contentView addSubview:b1];
[cell.contentView addSubview:b2];
[cell.contentView addSubview:b3];
[b1 release];
[b2 release];
[b3 release];
};
if([fileArray count]>0) //check if fileArray is correct
{
NSLog(@"___________________" );
for (int i=0;i<[fileArray count];i++)
{
NSLog(@"->%@",[[fileArray objectAtIndex:i] getfileName] );
}
}
UILabel *tem1UILabel=(UILabel*)[cell.contentView viewWithTag:row+15000];
NSString *s1=[[NSString alloc] initWithFormat: [[fileArray objectAtIndex:row] getfileName] ];
NSLog(@"--->%@",s1 );
tem1UILabel.text=s1;
[s1 release];
UILabel *tem2UILabel=(UILabel*)[cell.contentView viewWithTag:row+16000];
NSString *s2=[[NSString alloc] initWithFormat: [[fileArray objectAtIndex:row] getfileDate] ];
tem2UILabel.text=s2;
[s2 release];
UILabel *tem3UILabel=(UILabel*)[cell.contentView viewWithTag:row+17000];
NSString *s3=[[NSString alloc] initWithFormat: [[fileArray objectAtIndex:row] getfileSize]];
tem3UILabel.text=s3;
[s3 release];
return cell;
}
I set the breakpoint at AAA and found that when I loaded folder F, my application did stop at the breakpoint.
But I have removed all cells in atableview before call 'reloadData'.
I checked the content of fileArray which is the data source of atableview and found that everything is correct except the display on atableview.
Welcome any comment.
Thanks
interdev