gpt4 book ai didi

swift - 当我单击 UISegmentedControl 时,将其更改为 UITableViewCell

转载 作者:行者123 更新时间:2023-11-30 10:08:54 25 4
gpt4 key购买 nike

如何调用方法

internal func segconChanged(segcon: UISegmentedControl, var text:String){

switch segcon.selectedSegmentIndex {
case 0:
print("whoo")
return text = "clicked Trainings"
case 1:
print("yeahh")
return text = "clicked Daily"
default:
break
}
}

来自

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

我是 swift 的初学者。

我正在使用 UISegmentedControl 和 UITableView。当我单击 UISegmentedControl 时,将其更改为 UITableViewCell。请查看 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

的内部

我添加了segconChanged(UISegmentedControl(), text: "")来调用方法,但我认为这是错误的,实际上是行不通的。

请给我建议。

这就是全部代码。

import UIKit

class TrainingLogViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var segment: UISegmentedControl!
let tabLog: NSArray = ["Trainings", "Daily"]



override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate = self
myTableView.dataSource = self
segment.addTarget(self, action: "segconChanged:", forControlEvents: UIControlEvents.ValueChanged)

}

var text: String = ""

internal func segconChanged(segcon: UISegmentedControl, var text:String){

switch segcon.selectedSegmentIndex {
case 0:
print("whoo")
return text = "clicked Trainings"
case 1:
print("yeahh")
return text = "clicked Daily"
default:
break
}
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 10
}

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

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let text: String = ""
*segconChanged(UISegmentedControl(), text: "")
let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "LogTraining")
cell.textLabel?.text = text
cell.detailTextLabel?.text = "subtitle"
return cell

}



}

最佳答案

您不需要手动调用segconChanged()函数,因为当段更改时它会自动调用。因此,只需在段更改时重新加载表格 View ,检查其索引并相应地填充数据,即在 cellForRowAtIndexPath 方法中。

试试这个代码:

import UIKit

class TrainingLogViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var segment: UISegmentedControl!
let tabLog: NSArray = ["Trainings", "Daily"]

override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate = self
myTableView.dataSource = self
segment.addTarget(self, action: "segconChanged:", forControlEvents: UIControlEvents.ValueChanged)

}

func segconChanged(segcon: UISegmentedControl){
self.myTableView.reloadData()
switch segcon.selectedSegmentIndex {
case 0:
print("clicked Trainings")
case 1:
print("clicked Daily")
default:
break
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

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

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("LogTraining", forIndexPath: indexPath)
cell.textLabel?.text = (self.segment.selectedSegmentIndex == 0) ? tabLog[0] : tabLog[1]
cell.detailTextLabel?.text = "subtitle"
return cell

}
}

关于swift - 当我单击 UISegmentedControl 时,将其更改为 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302274/

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