iphone開發:線程刷新reloadDataiPhone軟體開發教學課程

在開發過程中,遇到需要加載一些龐大數據,比如:可能會頻繁的請求網絡,顯示圖片

而我們不可能等待所有的圖片加載完成後再去刷新表視圖!!

 

如何解決?個人嘗試瞭好幾次線程的問題。終於解決。就是在子線程中給主線程發送消息!

 

個人做法是這樣:

 

首先顯示n張圖片,n不是太大。這裡n=5.然後多出n的圖片放在子線程中處理完成後加載。

if([array count]>5)

            {

                [self performSelectorInBackground:@selector(show_list_item_in_background:) withObject:array];

            }

 

但是我想在每M張圖片的時候就刷新一下表視圖。這時候需要這樣做:

 

 

– (void)show_list_item_in_background:(NSArray*)array

{

    intindex = 5;

    intsecond_run = [array count];

    while(index < second_run)

    {

        NSDictionary *dictionary = [array objectAtIndex:index];

        [self get_and_show_more:dictionary];

        index++;

       

        if(index > 5&& index % 5== 0)

        {

            [self performSelectorOnMainThread:@selector(reload_table_view_on_main_thread) withObject:nilwaitUntilDone:NO];

        }

    }

    [table_view reloadData];

    [activity_view stopAnimating];

}

 

主線程調用reload_table_view_on_main_thread方法即可對表視圖進行刷新。

 

– (void)reload_table_view_on_main_thread

{

    [table_view reloadData];

}

 

摘自 雲懷空-abel

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *