计数函数在 Swift 2 中不起作用
Count function is not working in Swift 2
嗨,我是 swift 和 ios 的新手,这里有一个问题。我从 segementedController 调用了 3 个不同的 table 视图。
一切正常,但每次我按分段控制器上的任何选项卡时,它都会打印星期一。只统计了table1.
的数据
@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 }
编辑:每当 select 分段控制器的第二个选项卡
时应用程序崩溃
也许你可以简化一下
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableView.count
}
试试这个
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
}
}
嗨,我是 swift 和 ios 的新手,这里有一个问题。我从 segementedController 调用了 3 个不同的 table 视图。
一切正常,但每次我按分段控制器上的任何选项卡时,它都会打印星期一。只统计了table1.
的数据@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 }
编辑:每当 select 分段控制器的第二个选项卡
时应用程序崩溃也许你可以简化一下
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableView.count
}
试试这个
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
}
}