【发布时间】:2018-01-24 06:08:21
【问题描述】:
我是IOS新手,我正在使用swift。我想在单元格高度扩展时更改标签文本。请告诉我如何在swift中单元格高度扩展时更改文本。
导入 UIKit 类 ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource {
var selectedIndex = -1
var height = 54
// Data model: These strings will be the data for the table view cells
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
// cell reuse id (cells that scroll out of view can be reused)
let cellReuseIdentifier = "cell"
// don't forget to hook this up from the storyboard
@IBOutlet var tableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 2
}
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
// create a new cell if needed or reuse an old one
let cell:Cell1 = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell1!
cell.textLabel?.text = "-"
return cell
}
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let cell:Cell1 = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell1!
if(selectedIndex == indexPath.row)
{
height = 216
cell.textLabel?.text = "+"
}else{
height = 54
cell.textLabel?.text = "/"
}
return CGFloat(height)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
print("You tapped cell number \(indexPath.row).")
if(selectedIndex == indexPath.row)
{
selectedIndex = -1
}
else
{
selectedIndex = indexPath.row
}
}
}
【问题讨论】:
-
只需更改您调用代码的文本字段中的文本即可更改单元格高度 yourTextField.text = "required content"
-
请帮我编辑我的代码
-
我无法理解我在哪里更改文本
-
总结一下您的问题,您是否希望更改特定索引的文本?
-
是的,我想更改tableview扩展单元格的标签文本
标签: swift uitableview uilabel heightforrowatindexpath