PDA

View Full Version : Continue to reloadData in UITableView




forrestxu
Sep 21, 2008, 01:46 PM
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
Sep 21, 2008, 02:16 PM
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: (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject:waitUntilDone:).

forrestxu
Sep 21, 2008, 06:43 PM
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:@selector(display1) withObject:nil waitUntilDone:YES];
[self.tableView reloadData];
... ...
[self performSelectorOnMainThread:@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
Sep 22, 2008, 02:29 AM
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.