在UITableView中为每个部分添加特定数据-Swift [英] Adding specific data for each section in UITableView - Swift

查看:76
本文介绍了在UITableView中为每个部分添加特定数据-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Table视图,其中的数据是从Dictionary数组编译的,其中的键是节头:

I have a Table view with data compiled from a Dictionary array where the keys are the section headers:

var data: Dictionary<String,[String]> = [
    "Breakfast": ["Oatmeal","Orange Juice"],
    "lunch": ["Steak","Mashed Potatoes","Mixed Veg"],
    "Dinner": ["Chicken","Rice"],
    "Snack": ["Nuts","Apple"]

]
var breakfastCalories = [100,200,300]
var lunchCalories = [300,400,500]
var DinnerCalories = [600,700,800]
var breakfast = 0

下面是填充表格视图的代码

Below is the code for populating the Table View

override func viewDidLoad() {
    super.viewDidLoad()

    for value in breakfastCalories as NSArray as! [Int]{

    breakfast = breakfast + value

    }

}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return data.count
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Return the number of rows in the section.
    let sectionString = Array(data.keys)[section]

    return data[sectionString]!.count
}

 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let sectionString = Array(data.keys)[section]
    return sectionString
}


 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
    let sectionString = Array(data.keys)[indexPath.section]


    cell.caloriesLabel.tag = indexPath.row
    cell.caloriesLabel.text = String(breakfastCalories[indexPath.row])
    cell.foodLabel.tag = indexPath.row
    cell.foodLabel.text = data[sectionString]![indexPath.row]

    return cell
}

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
//    self.myTableView.tableFooterView = footerView;


    let label = UILabel(frame: CGRectMake(footerView.frame.origin.x - 15, footerView.frame.origin.y, footerView.frame.size.width, 20))
    label.textAlignment = NSTextAlignment.Right

    label.text = "Total Calories: \(breakfast) "

    footerView.addSubview(label)

    return footerView
}

 func tableView(tableView: UITableView, heightForFooterInSection section: Int) ->  CGFloat {

    return 20.0
}

我的问题是,如何为每个部分添加卡路里数组?因此,对于早餐,其中将包含来自BreakfastCalories数组,LunchCalories数组的午餐部分等的卡路里。

My question is, how can I add the calories array for each section? so for breakfast, it will contain the calories from the breakfastCalories array, lunch section for the lunchCalories array etc.

我可能对此有过多的思考,但我无法理解解决此问题

I may be overthinking this but I can't get my head around this problem

谢谢


在右侧,值是从breakfastCalories中获取的,但是如上所述,每个部分都包含了BreakfastCalories数组中的卡路里,LunchCalories数组中的午餐部分等。

On the right the values are grabbed from the breakfastCalories however as mentioned each section contain the calories from the breakfastCalories array, lunch section for the lunchCalories array etc.

推荐答案

您可以使用与您的 data 属性类似的方式构造卡路里,并使用类似的键:

You could structure your calories similar to your data property, with similar keys:

var calories: Dictionary<String,[Int]> = [
"Breakfast": [100,200,300],
"lunch": [300,400,500],
"Dinner": [600,700,800]
]

这样,您可以根据所显示的部分提取正确的卡路里并将其加起来以创建总计以供标签显示:(在创建footerView的位置添加此内容,并在设置label.text的位置上方添加内容)

That way you can pull out the right calories depending on what section you are showing and add them up to create a total for you label to show: (Add this where you create your footerView and just above where you set your label.text)

let dataKeysArray = Array(data.keys)[section]
let sectionString = dataKeysArray[section]
let mealCalories = calories[sectionString]

var totalCalories: Int = 0
for calories in mealCalories {
    totalCalories += calories
}

label.text = "Total Calories: \(totalCalories) "

这篇关于在UITableView中为每个部分添加特定数据-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆