【发布时间】:2011-07-31 11:41:03
【问题描述】:
我正在开发一个购物清单,其中所有 未购买 商品位于 UITable 的顶部,所有 已购买 商品位于底部。商品从 sqlite DB 中读取 当用户第一次点击购物清单商品时,表示用户已经购买了该商品。因此,我将标签更改为灰色并将单元格移动到底部,相应地在数据源中也是如此。 (我通过删除项目并插入来做到这一点)
但是当用户再次点击它时(意味着他们需要再次购买),我希望单元格移动到顶部。我怎么做?如何更新数据源?
这是我的代码
- (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];
}
【问题讨论】:
标签: iphone objective-c xcode sqlite uitableview