gpt4 book ai didi

Swift:如果数组具有不同的对象数,TableView 和 SegmentedController 会崩溃

转载 作者:行者123 更新时间:2023-11-28 08:43:37 26 4
gpt4 key购买 nike

我正在尝试在基于分段控件的 TableView 中加载不同的数组,但如果数组具有不同的对象数,应用程序会在滚动表格时崩溃并出现错误“ fatal error :数组索引超出范围”。 '如果所有 5 个数组包含相等数量的对象,则一切正常 :) 下面是我设置 tableview 的代码部分。我相信我这样做的方式有问题......

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows

var returnValue = 0


switch(segment.selectedSegmentIndex)
{
case 0:
returnValue = MeatData.count

case 1:
returnValue = BreadData.count


case 2:
returnValue = FishData.count

case 3:
returnValue = BevData.count

case 4:
returnValue = FruitsData.count

default: fatalError("unknown section")

}
return returnValue
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SegmentsCELL

let photoMeat = UIImage(named: "Meat")
let photoBread = UIImage(named: "Bread")
let photoFish = UIImage(named: "Fish")
let photoMilk = UIImage(named: "Bev")
let photoFruits = UIImage(named: "Fruits")

let meatIndex = MeatData[indexPath.row]
let fishIndex = FishData[indexPath.row]
let breadIndex = BreadData[indexPath.row]
let milkIndex = BevData[indexPath.row]
let fruitsIndex = FruitsData[indexPath.row]

switch(segment.selectedSegmentIndex)
{
case 0:


cell.nameCell.text = meatIndex.name
cell.calEmptyCell.text = String(meatIndex.cal)
cell.protEmptyCell.text = String(meatIndex.prot)
cell.fatEmptyCell.text = String(meatIndex.fat)
cell.carbsEmptyCell.text = String(meatIndex.val)
cell.imageCell.image = photoMeat


break
case 1:
cell.nameCell.text = breadIndex.name
cell.calEmptyCell.text = String(breadIndex.cal)
cell.protEmptyCell.text = String(breadIndex.prot)
cell.fatEmptyCell.text = String(breadIndex.fat)
cell.carbsEmptyCell.text = String(breadIndex.val)
cell.imageCell.image = photoBread

break
case 2:
cell.nameCell.text = fishIndex.name
cell.calEmptyCell.text = String(fishIndex.cal)
cell.protEmptyCell.text = String(fishIndex.prot)
cell.fatEmptyCell.text = String(fishIndex.fat)
cell.carbsEmptyCell.text = String(fishIndex.val)
cell.imageCell.image = photoFish

break
case 3:
cell.nameCell.text = milkIndex.name
cell.calEmptyCell.text = String(milkIndex.cal)
cell.protEmptyCell.text = String(milkIndex.prot)
cell.fatEmptyCell.text = String(milkIndex.fat)
cell.carbsEmptyCell.text = String(milkIndex.val)
cell.imageCell.image = photoMilk

break
case 4:
cell.nameCell.text = fruitsIndex.name
cell.calEmptyCell.text = String(fruitsIndex.cal)
cell.protEmptyCell.text = String(fruitsIndex.prot)
cell.fatEmptyCell.text = String(fruitsIndex.fat)
cell.carbsEmptyCell.text = String(fruitsIndex.val)
cell.imageCell.image = photoFruits

break
default:
break
}

return cell

}

@IBAction func segmentAction(sender: AnyObject) {
table.reloadData()
}

最佳答案

cellForRowAtIndexPath 中的这一部分导致了崩溃:

let meatIndex = MeatData[indexPath.row]
let fishIndex = FishData[indexPath.row]
let breadIndex = BreadData[indexPath.row]
let milkIndex = BevData[indexPath.row]
let fruitsIndex = FruitsData[indexPath.row]

将数据源项从数组中提取到适当的 case 部分。

例子

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SegmentsCELL

switch(segment.selectedSegmentIndex)
{
case 0:
let meatIndex = MeatData[indexPath.row]
let photoMeat = UIImage(named: "Meat")

cell.nameCell.text = meatIndex.name
cell.calEmptyCell.text = String(meatIndex.cal)
cell.protEmptyCell.text = String(meatIndex.prot)
cell.fatEmptyCell.text = String(meatIndex.fat)
cell.carbsEmptyCell.text = String(meatIndex.val)
cell.imageCell.image = photoMeat


break
case 1:

let breadIndex = BreadData[indexPath.row]
let photoBread = UIImage(named: "Bread")

cell.nameCell.text = breadIndex.name
cell.calEmptyCell.text = String(breadIndex.cal)
cell.protEmptyCell.text = String(breadIndex.prot)
cell.fatEmptyCell.text = String(breadIndex.fat)
cell.carbsEmptyCell.text = String(breadIndex.val)
cell.imageCell.image = photoBread

break
case 2:

let fishIndex = FishData[indexPath.row]
let photoFish = UIImage(named: "Fish")

cell.nameCell.text = fishIndex.name
cell.calEmptyCell.text = String(fishIndex.cal)
cell.protEmptyCell.text = String(fishIndex.prot)
cell.fatEmptyCell.text = String(fishIndex.fat)
cell.carbsEmptyCell.text = String(fishIndex.val)
cell.imageCell.image = photoFish

break
case 3:

let milkIndex = BevData[indexPath.row]
let photoMilk = UIImage(named: "Bev")

cell.nameCell.text = milkIndex.name
cell.calEmptyCell.text = String(milkIndex.cal)
cell.protEmptyCell.text = String(milkIndex.prot)
cell.fatEmptyCell.text = String(milkIndex.fat)
cell.carbsEmptyCell.text = String(milkIndex.val)
cell.imageCell.image = photoMilk

break
case 4:

let fruitsIndex = FruitsData[indexPath.row]
let photoFruits = UIImage(named: "Fruits")

cell.nameCell.text = fruitsIndex.name
cell.calEmptyCell.text = String(fruitsIndex.cal)
cell.protEmptyCell.text = String(fruitsIndex.prot)
cell.fatEmptyCell.text = String(fruitsIndex.fat)
cell.carbsEmptyCell.text = String(fruitsIndex.val)
cell.imageCell.image = photoFruits

break
default:
break
}

return cell

}

关于Swift:如果数组具有不同的对象数,TableView 和 SegmentedController 会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35849838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com