Continue to reloadData in UITableView

Discussion in 'iOS Programming' started by forrestxu, Sep 21, 2008.

  1. forrestxu macrumors newbie

    Joined:
    May 31, 2008
    #1
    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
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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:.
     
  3. forrestxu thread starter macrumors newbie

    Joined:
    May 31, 2008
    #3
    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
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     

Share This Page