我對錶格視圖有疑問。我已將標籤&切換到表視圖單元格中。我默認將開關值設置爲NO。現在,在模擬器中加載表視圖時,表視圖會顯示帶有NO值的開關。現在我選擇開關值爲YES。但是table view使用dequeue可重用的單元格方法當table view被滾動的時候屬性object每次都會重新加載,那麼switch value的值是NO還是YES?表格視圖數據加載問題
回答
它會是YES。 滾動tableview的另一件事情是它不調用reload方法。它只是重用已經創建的tableview單元格,如果你正在使用deque可重用單元格方法屬性。
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
}
希望有所幫助。
只是變化不大代碼在下面的方法,使用不同的標識-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];
}
我相信它會幫助你。
中所做的那樣,這只是繞過了單元重用。問題是視圖不應該擁有數據。設置交換機的狀態,並期望它保持這個是錯誤的。你應該有一個模型來記住狀態,然後控制器獲取該狀態,並在每次運行'tableView:cellForRowAtIndexPath:'配置時將其設置在視圖上。 –
保證不有重複單元: 使用此代碼在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
如下:
for(UIView *v in [cell subviews])
{
if([v isKindOfClass:[UILabel class]] ||[v isKindOfClass:[UISwitch class]])
[v removeFromSuperview];
}
- 1. 將數據表加載到數據網格視圖問題
- 2. 加載表格視圖數據
- 3. IOS加載數據表格視圖
- 4. IOS中的表視圖中的數據加載問題
- 5. 表視圖加載性能問題
- 6. 加載視圖問題
- 7. 在將表格視圖加載NSMutablearray值時出現問題?
- 8. 問題與數據網格視圖
- 9. 問題,同時存儲在數據庫到表視圖加載數據
- 10. iphone - 在視圖控制器中加載表視圖的問題
- 11. 圖表加載問題
- 12. 數據表網頁加載問題
- 13. JSON數據表視圖的iOS加載
- 14. 無法加載數據到表視圖
- 15. 數據加載到列表視圖
- 16. 表視圖中沒有加載數據
- 17. 在列表視圖中加載數據
- 18. 奇怪的問題加載數據到文本視圖字段
- 19. Java添加表格數據問題
- 20. 加載訪問數據表表格到數據表
- 21. 如何在表格視圖的兩部分加載數據iPhone
- 22. 無法在表格視圖中加載核心數據?
- 23. 當視圖出現時在表格中加載新數據
- 24. 滾動到底部後在表格視圖中加載數據
- 25. 重新加載數據不會刷新表格視圖
- 26. Swift在表格視圖控制器中重新加載數據
- 27. iOS的表格視圖只加載設備旋轉的數據
- 28. UINavigationController總是以相同的數據加載表格視圖
- 29. 數據加載問題
- 30. jqGrid數據加載問題
請一些代碼添加到您的問題,它很難看你具體問什麼。 –