【问题标题】:Shopping list, moving row to the top when selected购物清单,选中时将行移至顶部
【发布时间】: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


    【解决方案1】:

    您不应该删除和添加该项目。 向模型添加一个布尔属性“购买”。 显示单元格时,请确保从购买 = YES 的数组开始对数组进行排序;

    当您单击一个单元格时,更改购买的=!购买 在桌子上做 reloadData

    编辑:如果你想以正确的顺序购买,而不是 bool 使“购买”一个 int 并存储它的订单索引

    【讨论】:

    • 感谢您的回复。但是,当您重新加载数据时,它会在没有动画的情况下加载。能不能帮我看看效果如何?
    • afaik 动画仅在添加/删除时播放。没有为重新加载事件提供委托。
    • "如果你想以正确的顺序购买那些,而不是 bool 使"购买"一个 int 并存储它的订单索引"你能解释一下这个......你能给一个简单的例子?
    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 2021-10-12
    • 2023-04-05
    • 1970-01-01
    • 2020-03-08
    • 2021-02-05
    • 2020-08-06
    相关资源
    最近更新 更多