在復用TableView的時候,一定要從數據源的角度出發。以咱們這個問題為例,比如,我們的tableView數據源是timelineArray,保存著一個timeline列表。假如Button是“贊”這個按鈕的話,我們之所以要展示這個“贊”按鈕,是因為當前這條timeline中的isLiked(是否被贊)屬性為NO.所以這樣寫:-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{BtnTableViewCell*cell=(BtnTableViewCell*)[tableViewdequeueReusableCellWithIdentifier:@"BtnTableViewCell"];NSDictionary*timeline=[self.timelinesobjectAtIndex:indexPath.row];BOOLisLiked=[[timelineobjectForKey:@"isLiked"]boolValue];//tableViewCell的樣式是和數據源有關的,所以我們是根據數據源來確定樣式。為了閱讀方便寫成下面這樣,其實是可以寫成一行:cell.likeButton.hidden=!isLiked;if(isLiked){cell.likeButton.hidden=YES;}else{cell.likeButton.hidden=NO;}//我們最好再給button再加一個tag,方便標識之后點擊的時候確定點擊了哪條timelinecell.likeButton.tag=indexPath.row;returncell;}因為在滑動tableview時,就是通過cellForRowAtIndexPath來復用cell的,所以數據源的改變,cell的樣式也會改變。同時,我們進行任何操作時,本來就會更新數據源的最新狀態,這是最正確的做法。-(void)btn_tapped:(id)sender{if(![senderisKindOfClass:[UIButtonclass]){return;}UIButton*btn=(UIButton*)sender;//通過tag來獲取到timeline的indexNSIntegerrow=btn.tag;//【1】立即隱藏該按鈕;btn.hidden=YES;//【2】標記數據源,例如保存這條消息已經被贊過了,方便接下來復用cell時識別NSMutableDictionary*timeline=[self.timelinesobjectAtIndex:row];[timelinesetObject:[[NSNumberalloc]initWithBool:YES]forKey:@"isLiked"];}