作者peliuya (Unknown)
看板MacDev
標題Re: [問題] reloadRowsAtIndexPath exception
時間Mon Nov 4 17:33:03 2013
※ 引述《peliuya (Unknown)》之銘言:
: ※ 引述《uranusjr (←這人是超級笨蛋)》之銘言:
: : 當 textFieldShouldEndEditing 被呼叫時, text field 還沒有結束編輯
: : 這時候你還不能對包含它的 cell 進行 reload (正確來說應該是不能 remove)
: : 不然程式就...嗯, 失去了 consistency
: : 解決方法是不要在 textFieldShouldEndEditing: 直接呼叫 reload
: : 而是要在它「結束之後」排定一個 reload 事件
: : 最簡單的解法(之一)是用 GCD 塞一個 async call
: : http://stackoverflow.com/questions/13907335/
問題可能是出在 cellForRowAtIndexPath 內, 才有資料不同步的情況
cellForRowAtIndexPath
{
AlertThresholdCell *AlertCell= (AlertThresholdCell *)
[tableView dequeueReusableCellWithIdentifier:@"AlertCell"];
if (AlertCell == nil)
{
NSArray *nibs;
nibs = [[NSBundle mainBundle]loadNibNamed:@"AlertThreshold"
owner:self options:nil];
AlertCell = [nibs objectAtIndex:0];
}
AlertCell.delegate = self;
NSLog(@"%@", AlertCell.textField);
return AlertCell;
}
textFieldShouldEndEditing
{
NSIndexPath *AlertThresholdPath;
AlertThresholdPath = [NSIndexPath indexPathForRow:1 inSection:0];
NSArray *myArray = [NSArray arrayWithObjects:AlertThresholdPath, nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self.functionTable reloadRowsAtIndexPaths:myArray
withRowAnimation:UITableViewRowAnimationAutomatic];
NSLog(@"%@", textField);
return YES;
}
這兩處印出的textField是不同的,
直到下次編輯結束 cellForRowAtIndexPath 中,
AlertCell才會拿到上次編輯的textField
請問我該怎麼保持資料的一致性呢?
我在cellForRowAtIndexPath 是否撰寫得有問題呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.251.44.30
→ s8911124:你要把cell想成只是顯示資料的UI 11/04 22:52
→ s8911124:你只要在reloadRowsAtIndexPath前,更新你cell要顯示資料 11/04 22:52
→ s8911124:即可 11/04 22:52
推 s8911124:dequeueReusableCellWithIdentifier會把畫面上沒出現cell 11/04 22:57
→ s8911124:回收,所以textfield要拿上次的是會破壞它本身系統的機制 11/04 22:59
→ s8911124:textifle 註冊UIControlEventEditingChanged,在對應 11/04 23:03
→ s8911124:action,把資料先寫進去在reloadRowsAtIndexPath即可 11/04 23:04