【问题标题】:Adding specific data for each section in UITableView - Swift为 UITableView 中的每个部分添加特定数据 - Swift
【发布时间】:2016-07-23 02:02:48
【问题描述】:

我有一个表格视图,其中包含从字典数组编译的数据,其中键是节标题:

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

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

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
}

我的问题是,如何为每个部分添加卡路里数组?所以对于早餐,它将包含来自早餐卡路里数组的卡路里,午餐卡路里数组的午餐部分等。

我可能想多了,但我无法解决这个问题

谢谢

右侧的值是从早餐卡路里中获取的,但如前所述,每个部分都包含来自早餐卡路里数组的卡路里,午餐卡路里数组中的午餐部分等。

【问题讨论】:

    标签: ios arrays swift uitableview


    【解决方案1】:

    您可以使用类似的键来构建您的 calories 类似于您的 data 属性:

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

    这样您就可以根据您显示的部分提取正确的卡路里并将它们相加以创建一个总数供您显示的标签:(在您创建 footerView 的位置和设置标签的位置上方添加这个。文字)

    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) "
    

    【讨论】:

    • 这个解决方案确实有道理,但是我在 mealCalories 上遇到了一个意外发现的 nil 错误
    • 没关系,只是一个愚蠢的错误。我没有为我的零食添加卡路里,所以它设置为零。感谢您的快速解决方案
    • @JoeBenton 我有一个类似的问题,将单元格的总数添加到页脚中,你认为你可以帮助我吗stackoverflow.com/questions/59039423/…
    猜你喜欢
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多