2012-10-21 563 views
0

我對錶格視圖有疑問。我已將標籤&切換到表視圖單元格中。我默認將開關值設置爲NO。現在,在模擬器中加載表視圖時,表視圖會顯示帶有NO值的開關。現在我選擇開關值爲YES。但是table view使用dequeue可重用的單元格方法當table view被滾動的時候屬性object每次都會重新加載,那麼switch value的值是NO還是YES?表格視圖數據加載問題

+0

請一些代碼添加到您的問題,它很難看你具體問什麼。 –

回答

1

它會是YES。 滾動tableview的另一件事情是它不調用reload方法。它只是重用已經創建的tableview單元格,如果你正在使用deque可重用單元格方法屬性。

-1

TableViewCells在表視圖滾動時在必要時被破壞並創建。

當一個單元在離開可見屏幕區域時被破壞時,作爲單元子視圖的開關控件也會被破壞。

因此,當您回滾到之前設置的開關之一時,您實際看到的是UITableViewCell的另一個實例,其中添加了切換視圖,因此它看起來像切換值變回NO。

你需要的是第三件事,它記住了每個開關在每一行中的值。如果您顯示的是Core Data實體信息表,那麼也許您可以爲您的實體定義一個屬性,如「active」。在這種情況下,每次你改變開關值的時候,您的核心數據實體「活躍」屬性設置爲開關的值,例如:

-(UITableViewCell *)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 

    if(cell == nil) 
    { 
     // set switch control tag number (don't use 0) 
    } 

    // get switch control by tag number 

    // pseudocode 
    Engine *myEngine = [arrEngine objectAtIndex:indexPath.row]; 

    mySwitchControl.active = myEngine.active; 

    ... 
} 

// when you change switch value, remember to update your core data entity value 
-(void)updateSwitchValue:(BOOL) newValue 
{ 
    // update value 
} 

否則,你可以簡單地使用布爾值的NSMutableArray的識別哪一行每個開關應該是或否:

// my header file (.h file) 
@interface 
{ 
    NSMutableArray *arrSwitchValues; 
} 

// my implementation file (.m file) 
-(UITableViewCell *)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 

    if(cell == nil) 
    { 
     // set switch control tag number (don't use 0) 
    } 

    // get switch control by tag number 

    // pseudocode 
    BOOL switchVal = [arrSwitchValues objectAtIndex:indexPath.row]; 

    mySwitchControl.active = switchval; 

    ... 
} 

// when you change switch value, remember to update your array 
-(void)updateSwitchValue:(BOOL) newValue 
{ 
    // update value 
} 

希望有所幫助。

+1

前三段是完全錯誤的,那麼你開始有道理。細胞不會被破壞並重新創造,這就是整個重點。他們得到_reused_。因此,您需要一個模型結構,並且每次都需要從模型中設置開關狀態。 – jrturton

+0

@jrturton絕對是真的。那麼這個簡單的方法之一就是爲每個單元格設置不同的-2CellIdentifier,就像我在我的答案 – Kamarshad

-1

只是變化不大代碼在下面的方法,使用不同的標識-2每個細胞

-UITableViewCell *)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {  
//here you should need to create different CellIdentifier for each cell of TableView 

    NSString * cellIdentifier= [NsString stringWithFormat:@"cellIdentifier %d",[indexPath row]]; 
    if(cell == nil) 
    { 

    //create TableView cell if no cell created previously 
    //Here Create The UISWitch and Set Tag `[indexPath row]` 
    } 


    //Here You need To validate the Switch 
    // validate stored state of Switch as TableView gets Scrolled Up or Down. 

    if([[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%d",[indexPath row]]]) 
     { 
      [mySwitch setOn:YES]; 
     } 
    else 
    { 
      [mySwitch setOn:NO]; 
    } 
    // remaining code will go as it is 

}

編輯:由於Paul.s在這種情況下所描述的問題與上面的方法,所以你應該使用單獨的模型來保存Switch的狀態。與此相同的小解決方案是使用NSUserDefaults來保存UISwitch的狀態。

//This is The Action of UISwitch, called whenever UISwitch value Changed 
- (void)onOffSwitch:(UISwitch*)sender{ 
    //set True/False Flag As user Changed The Switch 
    if(sender.isOn) 
     [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:[NSString stringWithFormat:@"%d",[sender tag]]]; 
    else 
     [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:[NSString stringWithFormat:@"%d",[sender tag]]]; 

     [[NSUserDefaults standardUserDefaults] synchronize]; 
    } 

我相信它會幫助你。

+0

中所做的那樣,這只是繞過了單元重用。問題是視圖不應該擁有數據。設置交換機的狀態,並期望它保持這個是錯誤的。你應該有一個模型來記住狀態,然後控制器獲取該狀態,並在每次運行'tableView:cellForRowAtIndexPath:'配置時將其設置在視圖上。 –

-1

保證不有重複單元: 使用此代碼在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

如下:

for(UIView *v in [cell subviews]) 
{ 
    if([v isKindOfClass:[UILabel class]] ||[v isKindOfClass:[UISwitch class]]) 
     [v removeFromSuperview]; 
}