Re: [問題] reloadRowsAtIndexPath exception

看板MacDev作者 (Unknown)時間11年前 (2013/11/04 17:33), 編輯推噓1(106)
留言7則, 1人參與, 最新討論串4/4 (看更多)
※ 引述《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

11/04 22:52, , 1F
你要把cell想成只是顯示資料的UI
11/04 22:52, 1F

11/04 22:52, , 2F
你只要在reloadRowsAtIndexPath前,更新你cell要顯示資料
11/04 22:52, 2F

11/04 22:52, , 3F
即可
11/04 22:52, 3F

11/04 22:57, , 4F
dequeueReusableCellWithIdentifier會把畫面上沒出現cell
11/04 22:57, 4F

11/04 22:59, , 5F
回收,所以textfield要拿上次的是會破壞它本身系統的機制
11/04 22:59, 5F

11/04 23:03, , 6F
textifle 註冊UIControlEventEditingChanged,在對應
11/04 23:03, 6F

11/04 23:04, , 7F
action,把資料先寫進去在reloadRowsAtIndexPath即可
11/04 23:04, 7F
文章代碼(AID): #1ITsdITy (MacDev)
文章代碼(AID): #1ITsdITy (MacDev)