我做了一个有趣的应用程序,并想知道如何实现对应用程序的结账/筐系统。

基本上,的产品列表被从网络服务器中的XML文件,然后将其在一个UITableView控制器显示的形式拉动。在小区A抽头将用户带到该产品的更详细的概述。

该应用程序将需要能够获得的产品为箱型的东西,这就是我得到卡住了。

是否有任何代码样本,我可以在看看?

有帮助吗?

解决方案

如果您选择适合您的数据适当的格式这个任务很简单。在ObjC最自然的XML represantation是NSDictionaries / NSArrays树。在simlest情况下,必须的项目的NSArray(与您在tableview中显示)它们中的每个的NSDictionary包含“ITEMNAME”键,“ITEMPRICE”等,这是很容易在访问表项在阵列与细胞索引显示:

-(UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * item = [allItems objectAtIndex:indexPath.row];
    NSString * cellTitle = [item objectForKey:@"ItemName"];
    ...
}

当项目cliked打开你的DetailViewController并通过参照选择的项目:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
    MyDetailViewController * myDetailViewController = [[MyDetailViewController alloc] init];
     //MyDetailViewController should have a property of NSDictionary type, let it be "item"
    myDetailViewController.item = [allItems objectAtIndex:indexPath.row];
    //pass also ref to basket
    myDetailViewController.basket = basket;
    [self.myDetailViewController pushViewController:dvController animated:YES];
    [myDetailViewController release]; 

}

要详细示出控制器中的所有项目属性。

篮是NSMutableDictionaries的的NSMutableDictionary。每个斗提项关键是一些所有项目,例如“ITEMNAME”中独特的项目属性。每个斗提项目值应包含在原始数组引用项。在MyDetailViewController可以篮加项目,如:

-(void)addToBasket{
    NSDictionary * itemToAdd = [NSDictionary dictionaryWithObjectsAndKeys:
       item, @"item", // this is reference to item from original array
       [NSNumber numberWithInt:amount], @"amount", // or some other properties
       nil];    
       [self.basket setObject:itemToAdd forKey:[item objectForKey:@"ItemName"]]; 
}

当打开DetailViewController检查,如果篮已经包含CURENT文件:

if ([self.basket objectForKey:[item objectForKey:@"ItemName"])

和如果有的话,显示CURENT项已经添加到篮,并提供选项来移除或改变量。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top