gpt4 book ai didi

ios - 计数函数在 Swift 2 中不起作用

转载 作者:行者123 更新时间:2023-11-30 14:00:50 24 4
gpt4 key购买 nike

嗨,我是 swift 和 ios 的新手,在这里遇到了问题。我有 3 个不同的表格 View ,我从 segementedController 调用它们。

一切工作正常,但每次我按分段 Controller 上的任何选项卡时,它都会打印星期一。只统计表1的数据。

@IBOutlet weak var d1 : UITableView!
@IBOutlet weak var d2 : UITableView!
@IBOutlet weak var d3 : UITableView!
@IBOutlet weak var segementedControlAction: UISegmentedControl!


var Names = ["Name1", "Name2","Name3","Name4"]
var Images = ["1.jpg","2.jpg","5.jpg","8.jpg"]
var set = ["14","15","16","17"]

var Names1 = ["Name5", "Name6","Name7"]
var Images1 = ["6.jpg","4.jpg","7.jpg"]
var set1 = ["4","5","6"]

var Names2 = ["Name9", "Name12","Name13","Name14"]
var Images2 = ["11.jpg","21.jpg","51.jpg","81.jpg"]
var set2 = ["114","115","116","117"]


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count: Int = 0
tableView.layer.cornerRadius = 10
tableView.layer.masksToBounds = true

if (tableView == self.d1) {
print("Monday")
count = self.Names.count
}
else if (tableView == self.d2) {
print("Tuesday")
count = self.Names1.count
}
else if (tableView == self.d3) {
print("Wednesday")
count = self.Names2.count
}

return count
}


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

let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as! CustomTableViewCell

if segementedControlAction.selectedSegmentIndex == 0 {
cell.nameLabel.text = Names[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: Images[indexPath.row])
cell.setLabel.text = set[indexPath.row]

}

else if segementedControlAction.selectedSegmentIndex == 1 {

cell.nameLabel.text = Names1[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: Images1[indexPath.row])
cell.setLabel.text = set1[indexPath.row]
}


else {segementedControlAction.selectedSegmentIndex == 2

cell.nameLabel.text = Names2[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: Images2[indexPath.row])
cell.setLabel.text = set2[indexPath.row]

}

return cell }

编辑:每当我选择分段 Controller 的第二个选项卡时,应用程序就会崩溃

最佳答案

试试这个

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

tableView.layer.cornerRadius = 10
tableView.layer.masksToBounds = true

if segementedControlAction.selectedSegmentIndex == 0 {

print("Monday")

return self.Names.count
}

else if segementedControlAction.selectedSegmentIndex == 1 {

print("Tuesday")

return self.Names1.count

}
else if segementedControlAction.selectedSegmentIndex == 2 {
print("Wednesday")

return self.Names2.count

}
}

关于ios - 计数函数在 Swift 2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33016102/

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