Search code examples
iosuitableviewcellreloaddata

Adding table cell when last cell displayed. in ios


I set. When last cell displayed , add cell by threadProcess.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell      forRowAtIndexPath:(NSIndexPath *)indexPath
 {
    int nArrayCount;
    nArrayCount=[self.mAppGameList count];
    int row= (int)indexPath.row ;

    if(row == nArrayCount)
    {
        if(mSearchGame_Thread != nil)
            return;

        NextSearchCell *searchCell =(NextSearchCell *)cell;

        [searchCell.mActivityView startAnimating];

        NSThread *searchThread = [[NSThread alloc] initWithTarget:self
                                                         selector:@selector(searchNextThreadProc:) object:tableView];

        self.mSearchGame_Thread = searchThread;
        [searchThread release];
        [self.mSearchGame_Thread start];
        // start new search ...

    }

//thread method

  -(void)searchNextThreadProc:(id)param
 {

    UITableView *tableView=(id)param;

    NSMutableArray *newArray;

    newArray=[NSMutableArray arrayWithArray:self.mAppGameList];

    NSArray *pressedlist;
    nArrayCount=[self.mAppGameList count];

               .
               .
               .
   [newArray addObject:item];
   self.mAppGameList = newArray;

     [tableView reloadData];

     self.mSearchGame_Thread=nil;
 }

This way is problem.

  1. If I scroll table when tableview reload data, for a while tableview disappeared and appeard.

  2. If I touch cell while adding next cell , sometimes it occur bad exc memory . I think, it call tableView: didSelectRowAtIndexPath: method before reloading new table. so, table's data is not.

So, I want to replace reload tableview way.Is there any way? please help me.


Solution

  • Instead of using reloadData, you can add the new rows by animation using this method in UITableView:

    - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
    

    That way your view will not dissappear and reappear when reloading.

    See this question: UITableView add cell Animation

    Also make sure to perform any refreshments of the UI in the main thread by using:

    [self performSelectorOnMainThread:@selector(refreshMethodName) withObject:nil waitUntilDone:NO];
    

    A few comments about your code:

    • Make sure that your mAppGameList is either a retain-property or a copy-property if used as above in your code. Otherwise that could cause bad access.
    • You should make sure searchNextThreadProc is not called several times at once, or you could get timing and performance issues. It does not look thread safe.
    • Typically you should handle content data a bit more separated from the UITableView. See the table view as a tool for displaying a list of data that is already in place. It should not have to worry about searching for data etc. Instead use a separate class that holds the data you are working with in an NSMutableArray, that you keep filling up with data when you need to. This class can be triggered to start searching for new data by the tableView through a method call, but make sure that the refreshment process is thread safe and sustainable to multiple calls from the UI! 10xrefresh calls at once still means only 1 refresh at a time! (we don't want 10 simultaneous calls to a server for example) This content list should be completely separated from the UITableView's list.
    • When new data is available, tell the UITableView to refresh by creating a refresh method that you can call. When refreshing the UITableView, if the mAppGameList property is retain or copy, no need to re-add all data in the list. If you have an NSMutableArray in a separate class containing all data as suggested, simply use something like self.mAppGameList = [NSArray arrayWithArray:[yourClass gameList]]; (if you are using retain for mAppGameList)
    • When triggering the refresh of the UITableView, use performSelectorOnMainThread. To start a new background thread you can also use performSelectorInBackground instead of NSThread alloc etc.