聲明:我在iOS 8上,所以有機會這是一個錯誤。cellForRowAtIndexPath屬性更改更改多個單元格
我試圖以編程方式編輯特定事件後特定索引在UITableView中的單元格的backgroundColor。我使用下面的代碼:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:1 green:0.84 blue:0 alpha:1];
雖然這工作得很好,只要我滾動,我看到其他單元格與改變背景顏色。我認爲它是與下面的代碼在我- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法:
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
...
我懷疑,因爲所有的細胞都帶有這個標識產生的,莫名其妙的性能感到困惑(雖然風格不那麼適用於一切,只有隨機單元格,所以這是違背這一理論的)。謝謝您的幫助!
你應該永遠不會改變的cellForRowAtIndexPath之外的單元格的內容。細胞在從視圖中滾動出來並回到視圖中時被「回收」,並且在該方法中未做出的任何更改將在滾動時蒸發。 –