2011-04-07 30 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