Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

forrestxu

macrumors newbie
Original poster
May 31, 2008
16
0
Hi Guys,

My requirements are simple.

I would like scan files one by one in a specific folder by clicking a button. Whenever my code find a file, it needs to display the file name in the GUI until all the files are scanned.

Therefore I change a content for a UITableview object tableView and call

[self.tableView reloadData];

whenever a file is scanned.

But the problem is only the last content will be displayed. This means that only the last reloadData works. The other does not work.

I also tried to create threads to display the file name using above approach.
It still does not work.

How to solve the problem?

Thanks,

Forrest
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
You need to be doing the scan in a thread: if you call reloadData in a tight look it won't actually update the GUI as the run loop won't get a look in. But you need UI updates to happen on the main thread (I think). So you need to ensure your model layer is thread safe (you have a well separated MVC design right?) and then call reloadData on the main thread. This can be done using performSelectorOnMainThread:withObject:waitUntilDone:.
 

forrestxu

macrumors newbie
Original poster
May 31, 2008
16
0
Hi Robbieduncan;

Thank you for your reply.
I still don't understand your solution. My best understanding is to do the code like below:

Call the following code in main thread.

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{... ...
[self performSelectorOnMainThread:mad:selector(display1) withObject:nil waitUntilDone:YES];
[self.tableView reloadData];
... ...
[self performSelectorOnMainThread:mad:selector(display2) withObject:nil waitUntilDone:YES];
[self.tableView reloadData];
}

in model layer( to make things simple, i put all method id the same class), I call

- (void) display1
{
commentstr = @"abcdefg ...";
}


- (void) display2
{
commentstr = @"aaaaaaa ...";
}
- (NSString *)textForSectionWithIndex:(NSInteger)sectionIndex
{
switch (sectionIndex)
{
... ...
case kCommentsSectionIndex: return commentstr;
}
return nil;
}

After didSelectRowAtIndexPath finished, abcdefg is still not displayed.

I think call performSelectorOnMainThread is almost the same meaning to call display1 or display2 directly in this case.
Thank you for your help!

Forrest
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
1) Use code tags

2) You code makes no sense to me whatsoever. You said you were loading data in a separate thread and wanted to update the UI every time you had new data. I was expecting you to call reloadData on the main thread from the load thread.

I have no idea why you have put anything in the tableViewUITableView:didSelectRowAtIndexPath: method: that's called when the user touches a row. It will be called on the main thread anyway but is nothing to do with data loading. You should not be using it to reload data. I suggest you spend some time reading the documents and looking at the examples as this makes no sense whatsoever.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.