如何从数组到表视图部分获取特定数据?

时间:2019-05-10 19:06:13

标签: swift uitableview

我的视图控制器的初始化程序中有数据数组。我想将这些前5个数据获取到tableView的第一部分。 而后5个数据到tableView的第二部分。我在tableView中有5个部分。我将所有数据收集到5个部分中,而我并没有仅获得5个特定数据。

我尝试使用forEach(),但没有找到前5个数据。

我的tableView代码如下:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
    return "Breakfast"
    } else if section == 1 {
    return "Hamburger"
    } else if section == 2 {
        return "Drinks"
    } else if section == 3 {
        return "Dessert"
    }
    return "Salad"
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 5
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return foods.count

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood") as! MainFoodTitleTableViewCell

    if indexPath.section == 0 {

        foods.forEach {
            food in
            let food = foods[indexPath.row]

            cell.title = food.name
            cell.price = food.price

        }

    }
    if indexPath.section == 1 {

        foods.forEach {
            food in
            let food = foods[indexPath.row]

            cell.title = food.name
            cell.price = food.price

        }

    }
    return cell
}

这是我的食物数据来源:

 class MainViewController: UIViewController {

 @IBOutlet weak var mainTableView: UITableView!
 @IBOutlet weak var mainCollectionView: UICollectionView!

 let foodSource: FoodsDataSource
 required init?(coder aDecoder: NSCoder) {
    let foods = [
        Food(category: "Breakfast", name: "Hafta İçi Kahvaltısı", price: 18.0),
        Food(category: "Breakfast", name: "Pazar Kahvaltısı", price: 25.0),
        Food(category: "Breakfast", name: "Diyet Kahvaltı", price: 22.0),
        Food(category: "Breakfast", name: "Köy Kahvaltısı", price: 15.0),
        Food(category: "Breakfast", name: "Ege Kahvaltısı", price: 30.0),

        Food(category: "Hamburger", name: "Big TezBurger", price: 26.0),
        Food(category: "Hamburger", name: "TezRoyal", price: 24.0),
        Food(category: "Hamburger", name: "Double TezzBurger", price: 17.0),
        Food(category: "Hamburger", name: "TezChicken", price: 21.0),
        Food(category: "Hamburger", name: "Köfteburger", price: 28.0)]

self.foodSource = FoodsDataSource(foods: foods, images: imageNames)
    super.init(coder: aDecoder)
}

预期结果类似于第1部分为Breakfast,早餐菜单为OmeletteBaconPotatoeTomatoEgg < / p>

2 个答案:

答案 0 :(得分:1)

我建议使用两个结构作为数据模型

struct Category {
    let name : String
    let foods : [Food]
}

struct Food {
    let name : String
    let price : Double
}

let categories = [
    Category(name: "Breakfast", foods : [
        Food(name: "Hafta İçi Kahvaltısı", price: 18.0),
        Food(name: "Pazar Kahvaltısı", price: 25.0),
        Food(name: "Diyet Kahvaltı", price: 22.0),
        Food(name: "Köy Kahvaltısı", price: 15.0),
        Food(name: "Ege Kahvaltısı", price: 30.0)]),
    Category(name: "Hamburger", foods : [
        Food(name: "Big TezBurger", price: 26.0),
        Food(name: "TezRoyal", price: 24.0),
        Food(name: "Double TezzBurger", price: 17.0),
        Food(name: "TezChicken", price: 21.0),
        Food(name: "Köfteburger", price: 28.0)])
]

这大大减少了表视图数据源和委托方法中的代码

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return categories[section].name
}

func numberOfSections(in tableView: UITableView) -> Int {
    return categories.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return categories[section].foods.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood", for: indexPath) as! MainFoodTitleTableViewCell

    let category = categories[indexPath.section]
    let food = category.foods[indexPath.row]    
    cell.title = food.name
    cell.price = food.price
    return cell
}

您在cellForRowAt中的代码仍然毫无意义。每个部分中的每一行都调用一次此方法。

答案 1 :(得分:0)

创建一个代表您的部分的数组

let sections = ["Breakfast", "Hamburger", "Drinks", "Dessert", "Salad"]

然后将功能更改为

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sections[section]
}

func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let sectionName = sections[section]
        return foods.filter({ $0.category == sectionName }).count

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CellForFood") as! MainFoodTitleTableViewCell

    food = foods.filter({ $0.name == sections[indexPath.section] })[indexPath.row]
    cell.title = food.name
    cell.price = food.price

    return cell
}