2011-04-07 18 views
0

我將所有買下的購物清單退回將物品置於UITable的頂部,並且所有買下物品將處於底部。這些項目是從sqlite數據庫中讀取的當用戶第一次點擊購物清單項目時,這意味着用戶已經購買了該項目。因此,我將標籤更改爲灰色,並將單元格移到底部,相應地也在數據源中。 (我已經通過刪除該項目並插入來完成此操作)購物清單,選中時將排移到頂部

但是,當用戶再次點擊它(意味着他們需要再次購買)時,我希望單元格移動到頂部。我怎麼做?我如何更新數據源?

這裏是我的代碼

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
Item *item=[[appDelegate.slist objectAtIndex:indexPath.row]retain]; 
NSUInteger index=indexPath.row; 
NSUInteger lastindex=appDelegate.slist.count-1; 

//get the cell 
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath]; 

if (item.iNeed==1) { 
    cell.textLabel.textColor=[UIColor lightGrayColor]; 
    if (index!=lastindex) { 
     [appDelegate deleteSLItem:item]; //delete from DB 
     item.iNeed=0; 
     [appDelegate insertSLItem:item]; //insert again 

    //for animation 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
     [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastindex inSection:0]] withRowAnimation:UITableViewRowAnimationLeft]; 
     [tableView endUpdates]; 
    } 
}else if (item.iNeed==0) { 
    item.iNeed=1; 
    cell.textLabel.textColor=[UIColor blackColor]; 
/* 
i want to know what am i supposed to do here to move the cell to the top and 
correspondingly in the data source as well 
*/ 
    [appDelegate updateSLItem:item]; 
} 
[item release]; 
} 

回答

1

你不應該刪除和添加的項。 將「購買」的布爾屬性添加到模型中。 當顯示單元格時,請確保將數組從排序開始進行排序,使用= =; !

當您單擊更改單元格的買=買 待辦事項reloadData在桌子上

編輯:如果你想擁有的,而不是一個布爾值作出正確的順序買的,「買」一個int並存儲它的訂單索引

+0

感謝您的回覆。但是當你重新加載數據時,它會在沒有動畫的情況下加載。你能幫助我如何顯示效果嗎? – Neelesh 2011-04-07 09:27:37

+1

afaik動畫僅在添加/刪除時播放。沒有爲重裝事件提供委託。 – 2011-04-07 09:41:53

+0

「如果你想買正確的順序,而不是一個布爾make」買「一個整數,並存儲它的訂單指數」 「你能解釋這個更多一點..你可以舉個簡單的例子嗎? – Neelesh 2011-04-07 09:51:06

相關問題