TableView multiple cell doesnt work, Terminated due to signal 9

时间:2018-12-03 13:10:37

标签: ios swift uitableview tableview

I wanted to make when tabbaritem click call api and fill tableView. But in my case i had to use multiple cell.

 up category (tab bar item)  -> category (1- cell) -> products (2- cell)
 up category (tab bar item)-> products (2- cell)

i have two prototype cell in tableView. First cell for show category. Second cell for show products. When category click, i reloaded tableview then i can show products. But when i showed products just only 1 time. My tableview unavaliable. I am sure i call reload data. Message from debugger: Terminated due to signal 9 Here's my code,

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if let orderType = selectedOrderType
    {
        switch orderType
        {
            case OrderViewHelper.OrderType.Category:
                  return self.categories.count;
            case OrderViewHelper.OrderType.Menu:
                  return self.menus.count;
            case OrderViewHelper.OrderType.Product:
                  return self.products.count;
        }
    }
    return 0;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    var returnerCell = UITableViewCell();
    switch selectedOrderType!
    {
        case OrderViewHelper.OrderType.Category:
                let cell = tableView.dequeueReusableCell(withIdentifier: ViewCellStatics.TABLE_ORDER_VIEW_CELL, for: indexPath) as! CategoryTableViewCell;
                cell.lblCategoryId.text = self.categories[indexPath.row].Id;
                cell.lblCategoryName.text = self.categories[indexPath.row].Name;
                cell.lblCategoryId.isHidden = true;
                returnerCell = cell;
                break;
        case OrderViewHelper.OrderType.Menu:
            let cell = tableView.dequeueReusableCell(withIdentifier: ViewCellStatics.TABLE_PRODUCT_VIEW_CELL, for: indexPath) as! ProductsTableViewCell;
            cell.lblPrice.text = self.menus[indexPath.row].Price;
            cell.lblProductName.text = self.menus[indexPath.row].Name;
            cell.lblId.text = self.menus[indexPath.row].Id;
            cell.lblId.isHidden = true;
            cell.lblProductName.numberOfLines = 2;
            returnerCell = cell;
            break;
        case OrderViewHelper.OrderType.Product:
            let cell = tableView.dequeueReusableCell(withIdentifier: ViewCellStatics.TABLE_PRODUCT_VIEW_CELL, for: indexPath) as! ProductsTableViewCell;
            cell.lblPrice.text = self.products[indexPath.row].Price;
            cell.lblProductName.text = self.products[indexPath.row].Name;
            cell.lblId.text = self.products[indexPath.row].Id;
            cell.lblId.isHidden = true;
            cell.lblProductName.numberOfLines = 2;
            cell.addButton.Model = self.products[indexPath.row];
            cell.addButton.addTarget(self, action: #selector(addBasket(_:)), for: .touchUpInside);
            break;
    }
    return returnerCell;
}



private func getCategories(upperCategoryId : String)
    {
        var paramaters = [WebServiceParamater]();
        paramaters.append(WebServiceParamater(key: WebServiceVariableKeys.UPPER_CATEGORY, value: upperCategoryId));
        WebService().GetData(action: ActionNames.CATEGORIES, paramater: paramaters)
        {
            (objects) in
            if (objects == nil || objects?.count == 0) { return; }
            self.ClearModels(type: OrderViewHelper.OrderType.Category);
            DispatchQueue.main.sync
            {
                self.categories = JsonParser.ParseCategories(jsonArray: objects);
                self.tableOrders.reloadData();
            }
        }
    }

    private func getProducts(category : CategoryModel)
    {
        var paramaters = [WebServiceParamater]();
        paramaters.append(WebServiceParamater(key: WebServiceVariableKeys.CATEGORY, value: category.Id));
        WebService().GetData(action: ActionNames.PRODUCT, paramater: paramaters)
        {
            (objects) in
            if (objects == nil || objects?.count == 0) { return; }
            self.ClearModels(type: OrderViewHelper.OrderType.Product);
            DispatchQueue.main.sync
            {
                self.products = JsonParser.ParseProduct(jsonArray: objects);
                self.tableOrders.reloadData();
            }
        }
    }

Screenshots

Category

enter image description here

I clicked one category so i can see products

enter image description here

Reload data doesnt work.

enter image description here

1 个答案:

答案 0 :(得分:0)

Never call DispatchQueue.main.sync, try to DispatchQueue.main.async instead.

BTW: Make sure your methods in WebService calling asynchronously in background threads so you won't block UI while loading something.

Hope this answer will help you in understandings https://stackoverflow.com/a/44324968/4304998