购物清单,选择时将行移动到顶部

发布于 2024-10-31 02:36:59 字数 1535 浏览 0 评论 0原文

我正在开发一个购物清单,其中所有未购买项目位于 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];
}

I am devleoping a shopping list with all the unbought items to be on the top of the UITable and all the bought items to be on the bottom. The items are read from sqlite DB When the user first clicks on a shopping list item, it means the user has bought the item. Hence i change the label to grey color and move the cell to the bottom,correspondingly in the data source as well. (i have done this by deleting the item and inserting)

However when the user clicks on it again (meaning they need to buy this again), i want the cell to move to the top. How do i do that? How do i update the datasource ?

Here is my code

- (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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2024-11-07 02:36:59

您不应删除并添加该项目。
将布尔属性“bought”添加到模型中。
显示单元格时,请确保从带有 buy = YES; 的单元格开始对数组进行排序。

当您单击一个单元格时,更改 buy=!bought
在表上执行 reloadData

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

You shouldn't delete and add the item.
Add a bool attribute "bought" to the model.
When displaying the cells make sure to sort your array starting with the ones with bought = YES;

When you click a cell change the bought=!bought
Do reloadData on the table

EDIT: if you want to have the bought ones in the correct order, instead of a bool make "bought" an int and store it's order index

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文