UITableViewCell部分设置并显示数据到部分

时间:2017-02-28 05:58:00

标签: ios objective-c uitableview

我有像这样的主阵列(CartPProductListArray

  data=
 {
  data =  (
            {
        "cart_id" = 760;
        "cart_quantity" = 1;
        "image_url" = "http://web-medico.com/web2/kibbeh/uploads/14776327941523279103.jpg";
        name = "Milk Test ";
        price = "2.34";
        "product_id" = 44;
        "store_id" = 3;
    },
            {
        "cart_id" = 760;
        "cart_quantity" = 1;
        "image_url" = "http://web-medico.com/web2/kibbeh/uploads/14776327911430225702.jpg";
        name = "Fish Test ";
        price = "2.34";
        "product_id" = 43;
        "store_id" = 3;
    },
            {
        "cart_id" = 760;
        "cart_quantity" = 1;
        "image_url" = "http://web-medico.com/web2/kibbeh/uploads/14877289882050445800.jpg";
        name = "Cortas Apricot Jam ";
        price = "3.62";
        "product_id" = 77;
        "store_id" = 7;
    }
);
msg = "Data Found";
status = 1;
}

现在,我明智地过滤了这两个数组( store_id ) LebaneseStoreArray( store_id = 3)和BabylonStoreArray( store_id = 7)

lenbanese==(
    (
            {
        "cart_id" = 760;
        "cart_quantity" = 1;
        "image_url" = "http://web-medico.com/web2/kibbeh/uploads/14776327941523279103.jpg";
        name = "Milk Test ";
        price = "2.34";
        "product_id" = 44;
        "store_id" = 3;
    },
            {
        "cart_id" = 760;
        "cart_quantity" = 1;
        "image_url" = "http://web-medico.com/web2/kibbeh/uploads/14776327911430225702.jpg";
        name = "Fish Test ";
        price = "2.34";
        "product_id" = 43;
        "store_id" = 3;
       }
    )
)
 babylon==(
    (
    {
        "cart_id" = 760;
        "cart_quantity" = 1;
        "image_url" = "http://web-medico.com/web2/kibbeh/uploads/14877289882050445800.jpg";
        name = "Cortas Apricot Jam ";
        price = "3.62";
        "product_id" = 77;
        "store_id" = 7;
      }
  )
)

在我的UITableViewCell中有两个部分( labansese和babylon ) 这是我在numberOfRowsInSection

中的代码
if(section == 1)
{
    return LebaneseStoreArray.count;
}
else if (section == 2)
{
    return BabylonStoreArray.count;
}
else
{
    return CartPProductListArray.count;
}

但是,它得到了这个(检查图像)enter image description here

我希望明智的产品能在购物车中存放。

请帮我解决这个问题

提前感谢。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CartCustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"CartCell"];
    if (Cell == nil)
    {
        Cell = [[CartCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CartCell"];
    }

    Cell.ProductNameLabel.text = [[CartPProductListArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    Cell.ProductPriceLabel.text =[NSString stringWithFormat:@"$%@",[[[CartPProductListArray objectAtIndex:indexPath.row] objectForKey:@"price"] stringValue]];
    Cell.ProductQuantityTextField.text =[[[CartPProductListArray objectAtIndex:indexPath.row] objectForKey:@"cart_quantity"] stringValue];

    [Cell.ProductImageview sd_setImageWithURL:[NSURL URLWithString:[[CartPProductListArray objectAtIndex:indexPath.row]objectForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"logo.png"]];

    [Cell.PlusQuantityButton addTarget:self action:@selector(PlusQuantityButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    Cell.PlusQuantityButton.tag = indexPath.row;
    [Cell.MinunQuantityButton addTarget:self action:@selector(MinusQuantityButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    Cell.MinunQuantityButton.tag = indexPath.row;

    return Cell;
}

帮助我解决ios中的新问题。

3 个答案:

答案 0 :(得分:0)

您可以使用NSSortDescriptor对数组进行排序。

let sortDescriptor = NSSortDescriptor(key:"storeID",ascending:true,selector: #selector(NSString.localizedCompare(_:)))

答案 1 :(得分:0)

只需在日志中打印数组即可。如果你得到当前数组。 我认为问题出在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // here your code}

检查数组是否在indexpath的cellfor行内加载。 //它总是加载第一个arrary。更改数组取决于部分。

答案 2 :(得分:0)

您必须像这样更新numberOfRowsInSection

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return LebaneseStoreArray.count;
    }
    else if (section == 1)
    {
        return BabylonStoreArray.count;
    }
    else
    {
        return 0;
    }
}

对于Cell for Row,请使用以下代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CartCustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"CartCell"];
    if (Cell == nil)
    {
        Cell = [[CartCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CartCell"];
    }

    // For Section 1 : Lebanese Store Data
    if (indexPath.section == 0) {
        Cell.ProductNameLabel.text = [[LebaneseStoreArray objectAtIndex:indexPath.row] objectForKey:@"name"];
        Cell.ProductPriceLabel.text =[NSString stringWithFormat:@"$%@",[[[LebaneseStoreArray objectAtIndex:indexPath.row] objectForKey:@"price"] stringValue]];
        Cell.ProductQuantityTextField.text =[[[LebaneseStoreArray objectAtIndex:indexPath.row] objectForKey:@"cart_quantity"] stringValue];

        [Cell.ProductImageview sd_setImageWithURL:[NSURL URLWithString:[[LebaneseStoreArray objectAtIndex:indexPath.row]objectForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"logo.png"]];

    }else if (indexPath.section == 0){ // For Section 2 : Babylon Store Data
        Cell.ProductNameLabel.text = [[BabylonStoreArray objectAtIndex:indexPath.row] objectForKey:@"name"];
        Cell.ProductPriceLabel.text =[NSString stringWithFormat:@"$%@",[[[BabylonStoreArray objectAtIndex:indexPath.row] objectForKey:@"price"] stringValue]];
        Cell.ProductQuantityTextField.text =[[[BabylonStoreArray objectAtIndex:indexPath.row] objectForKey:@"cart_quantity"] stringValue];

        [Cell.ProductImageview sd_setImageWithURL:[NSURL URLWithString:[[BabylonStoreArray objectAtIndex:indexPath.row]objectForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"logo.png"]];
    }

    [Cell.PlusQuantityButton addTarget:self action:@selector(PlusQuantityButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    Cell.PlusQuantityButton.tag = indexPath.row;
    [Cell.MinunQuantityButton addTarget:self action:@selector(MinusQuantityButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    Cell.MinunQuantityButton.tag = indexPath.row;

    return Cell;
}